我将Spring与Spring-Batch vesrion 4.0.1和Spring-Boot verion 2.0.6一起使用
在带有转换的作业构建中使用Step Bean时,在@JobScope或@StepScope批注上使用它们时,我会遇到一些问题。
我将通过一个简单的例子来说明我的问题:
@EnableBatchProcessing
@Configuration
public class ExampleJobConfig {
private static final Logger LOG = LoggerFactory.getLogger(ExampleJobConfig.class);
@Autowired
private JobBuilderFactory jobBuilderFactory;
@Autowired
private StepBuilderFactory stepBuilderFactory;
@Bean
public Job simpleExampleJob() {
return jobBuilderFactory.get("simpleExampleJob")
.start(simpleExampleStep(null)).on("COMPLETED").end()
.from(simpleExampleStep(null)).on("*").fail()
.end()
.build();
}
@Bean
@JobScope
public Step simpleExampleStep(@Value("#{jobParameters['myParam']}") String myParam) {
return stepBuilderFactory.get("simpleExampleStep")
.tasklet(simpleExampleTasklet(myParam))
.build();
}
public Tasklet simpleExampleTasklet(String param) {
return (contribution, chunkContext) -> {
LOG.debug(param);
return RepeatStatus.FINISHED;
};
}
}
这段代码抛出org.springframework.beans.factory.BeanCreationException:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.simpleExampleStep': Scope 'job' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is java.lang.IllegalStateException: No context holder available for job scope
...
Caused by: java.lang.IllegalStateException: No context holder available for job scope
...
但是使用此作业声明,此代码可以正常工作:
@Bean
public Job simpleExampleJob() {
return jobBuilderFactory.get("simpleExampleJob")
.start(simpleExampleStep(null))
.build();
}
我不理解为什么它不适用于转换,因为这适用于相同的代码但没有转换。
您能帮我理解吗:)?