我正在使用Spring批处理4并且有一个包含一个tasklet的4个步骤的作业定义 . 定义的作业范围bean很少 . 我的理解是作业范围的bean对于作业实例是唯一的 . 使用每次运行唯一的作业参数调用作业 .
现在问题是在一个作业实例中创建的作业范围bean进入另一个作业实例 . 场景是第一个tasklet创建一个具有作业范围的bean,并且可以在另一个tasklet中访问它 . 当调用多个作业实例时,当job instance-2的step2 tasklet访问作业范围的bean时,它将被注入由job instance-1创建的bean . 理想情况下,应该已经传递了在作业实例-2中创建的bean .
Job definition:
<batch:job id="batchJob">
<batch:step id="step1">
<tasklet ref="tasklet1" />
<batch:next on="*" to="step2"/>
</batch:step>
<batch:step id="step2">
<tasklet ref="tasklet2" />
<batch:next on="*" to="step3"/>
</batch:step>
<batch:step id="step3">
<tasklet ref="tasklet3" />
<batch:next on="*" to="step4"/>
</batch:step>
<batch:step id="step4">
<tasklet ref="tasklet4" />
</batch:step>
</batch:job>
job scoped bean definition:
<bean id="testVo" class="com.test.model.report.TestVo" scope="job"/>
<bean class="org.springframework.batch.core.scope.JobScope" />
Job Launching through the job parameter:
JobParameters jobParameters =
new JobParametersBuilder()
.addLong("jobStartTime", System.currentTimeMillis())
.addLong("requestId", Long.valueOf(payload)).toJobParameters();
jobLauncher.run(batchJob, jobParameters);
示例Tasklet定义及其对作业范围bean的访问
@Component
public class Tasklet2 implements Tasklet {
/** The job scoped bean. */
@Autowired
private TestVo testVo;
....
}
上述配置中是否存在任何特定问题 . 为什么作业范围的bean在作业实例中混合?