![](/img/trans.png)
[英]SPRING-BATCH ERROR: No context holder available for step scope
[英]“Step” or “Job” Scope for Spring-Batch beans?
我正在使用Spring-Batch v3.0.0进行批量导入。 有一个StepScope
和一个JobScope
。 我怎么知道哪一个合适?
例如,如果我定义应该使用特定EntityManager
的自定义ItemReader
或ItemWriter
,它可能如下所示:
@Bean
@Scope("step") //@Scope("job") //custom scope required to inject #jobParameters
public JpaItemWriter<T> jpaItemWriter(EntityManagerFactory emf) {
JpaItemWriter<T> writer = new JpaItemWriter<T>();
writer.setEntityManagerFactory(emf);
return writer;
}
但是哪个范围就在这里? 为什么?
使用step
范围执行有效,但我觉得itemWrite应该是job
范围,这样就不会在每一步都重新创建它们。
我尝试将step
切换到job
,但是会抛出以下错误: Exception in thread "main" java.lang.IllegalStateException: No Scope registered for scope 'job'
从Spring-Batch v3.0.1开始,您可以使用@JobScope
将@Bean标记为@JobScope相当于将其标记为@Scope(value =“job”,proxyMode = TARGET_CLASS)
得到它:必须在@Configuration
文件中提供作为bean显式的范围。
@Bean
public JobScope jobScope() {
return new JobScope();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.