繁体   English   中英

Spring-Batch bean的“Step”或“Job”范围?

[英]“Step” or “Job” Scope for Spring-Batch beans?

我正在使用Spring-Batch v3.0.0进行批量导入。 有一个StepScope和一个JobScope 我怎么知道哪一个合适?

例如,如果我定义应该使用特定EntityManager的自定义ItemReaderItemWriter ,它可能如下所示:

@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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM