【发布时间】:2018-10-01 14:43:27
【问题描述】:
我正在构建一个基于 Spring Framework 的库,我希望允许用户并行调用库的方法。
在我的主类中,我自动装配服务类:
@Autowired
private ExportListCommand exportList;
这就是图书馆方法的实现:
public ResponseContainer<ExportListResponse> exportList(ExportListOptions options) {
exportList.setoAuthClient(oAuthClient);
ResponseContainer<ExportListResponse> result = exportList.executeCommand(options);
return result;
}
ExportListCommand 被定义为一个 Bean:
@Bean
@Scope("prototype")
public ExportListCommand exportList() {
return new ExportListCommand();
}
当我作为库用户并行运行 2 个 exportList 的方法时,Spring 只创建一个 ExportListCommand bean,因为它只自动装配一次。但实际上我需要 2 个独立的 ExportListCommand bean。我还尝试将@Scope(value="prototype") 更改为@Scope(value="prototype", proxyMode=ScopedProxyMode.TARGET_CLASS),但这也无法按我的需要工作:Spring 为每个方法调用创建ExportListCommand bean,并且由于我得到新对象而丢失了oAuthClient 值。
我让它只适用于我想避免的 AnnotationConfigApplicationContext.getBean() 方法。
我的选择是什么?谢谢。
【问题讨论】:
标签: java spring inversion-of-control