我正在尝试自动连接一个我标记为原型的 bean。因为我需要在每次迭代下创建新对象。但是不是创建新对象,而是当我在 for 循环中调用 setter 方法时,它会不断使用新值更新同一对象。我不确定我做错了什么.请指教。
下面的 Bean 配置类
@Configuration
public class DBConfigDev {
@Bean
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public DuplexWorker prototypeDuplexBean(){
return new DuplexWorker();
}
}
豆类如下
public class DuplexWorker implements Callable {
static final Logger logger = LoggerFactory.getLogger(DuplexWorker.class);
private List<PrintJobItem> printJobItems;
private List<String> groupIds;
private ArrayList duplexJobs;
private String groupId;
private CountDownLatch latch;
public DuplexWorker() {
}
@Override
public Object call() throws Exception {
this.latch.countDown();
return null;
}
/**
* @return the duplexJobs
*/
public ArrayList getDuplexJobs() {
return duplexJobs;
}
/**
* @param duplexJobs the duplexJobs to set
*/
public void setDuplexJobs(ArrayList duplexJobs) {
this.duplexJobs = duplexJobs;
}
/**
* @return the groupId
*/
public String getGroupId() {
return groupId;
}
/**
* @param groupId the groupId to set
*/
public void setGroupId(String groupId) {
logger.info("Going to Set GroupID :::"+this.groupId+":::"+Thread.currentThread().getName()+":::"+groupId);
this.groupId = groupId;
}
/**
* @return the latch
*/
public CountDownLatch getLatch() {
return latch;
}
/**
* @param latch the latch to set
*/
public void setLatch(CountDownLatch latch) {
this.latch = latch;
}
}
Autowire 类,它不会在下面创建新对象,我希望在 for 循环中调用 setter 方法将创建新对象
public class ProcessSimplex extends PrintBatchConstants implements Tasklet {
@Autowired
private DuplexWorker prototypeDuplexBean;
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
ArrayList<String> grpIds=(ArrayList)chunkContext.getStepContext().getJobExecutionContext().get(batchGroupIds);
/* grpIds.clear();
grpIds.add("86954");
*/
CountDownLatch latch = new CountDownLatch(grpIds.size());
for (int i = 0; i < grpIds.size(); i++) {
String[] args = new String[5];
args[0]= PAPER_COLOR;
args[1]= PAPER_SIZE;
args[2]= DELIVERY_MODE;
args[3]= PLEX;
args[4]= grpIds.get(i);
ArrayList simplexJobs = null;
ArrayList duplexJobs = null;
ArrayList allJobs = null;
allJobs = CfBatchPrintUtil.getJobs(args);
logger.info("AllJobs size "+allJobs.size());
simplexJobs = CfBatchPrintUtil.getPrintJobsByType(allJobs, false);
duplexJobs = CfBatchPrintUtil.getPrintJobsByType(allJobs, true);
logger.info("SimplexJobs size "+simplexJobs.size());
logger.info("duplexJobs size "+duplexJobs.size());
prototypeDuplexBean.setDuplexJobs(duplexJobs);
prototypeDuplexBean.setLatch(latch);
prototypeDuplexBean.setGroupId(args[4]);
service.submit(prototypeDuplexBean);
}
原型"意味着Spring Bean Factory每次被要求DuplexWorker
Bean时都会创建一个新实例。
每当 Spring 创建新的ProcessSimplex
实例时,都会分配@Autowired
字段prototypeDuplexBean
。
由于ProcessSimplex
可能是"单例",因此它只创建一次,因此自动连线字段prototypeDuplexBean
只分配一次,因此只创建DuplexWorker
的一个实例。
如果另一个类也自动连线DuplexWorker
,它将得到一个不同的实例。
如果ProcessSimplex
是"原型"bean,则ProcessSimplex
的每个实例都将使用不同的DuplexWorker
实例自动连接。由于问题代码没有显示ProcessSimplex
是如何实例化的,因此我们不知道这是否适用。