简体   繁体   English

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

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

I'm using Spring-Batch v3.0.0 for batch imports. 我正在使用Spring-Batch v3.0.0进行批量导入。 There is a StepScope and a JobScope . 有一个StepScope和一个JobScope How can I know which of them is appropriate? 我怎么知道哪一个合适?

For example, if I define a custom ItemReader or ItemWriter that should use a specific EntityManager , it could look like this: 例如,如果我定义应该使用特定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;
}

But which scope is right here? 但是哪个范围就在这里? And why? 为什么?

Execution with step scope works, but I feel the itemWriters should maybe be of job scope so that they are not recreated on every step. 使用step范围执行有效,但我觉得itemWrite应该是job范围,这样就不会在每一步都重新创建它们。

I tried switching step to job , but that throws following error: Exception in thread "main" java.lang.IllegalStateException: No Scope registered for scope 'job' 我尝试将step切换到job ,但是会抛出以下错误: Exception in thread "main" java.lang.IllegalStateException: No Scope registered for scope 'job'

Since Spring-Batch v3.0.1 you can use @JobScope 从Spring-Batch v3.0.1开始,您可以使用@JobScope

Marking a @Bean as @JobScope is equivalent to marking it as @Scope(value="job", proxyMode=TARGET_CLASS) 将@Bean标记为@JobScope相当于将其标记为@Scope(value =“job”,proxyMode = TARGET_CLASS)

Got it: one has to provide the scope as a bean explicit within the @Configuration file. 得到它:必须在@Configuration文件中提供作为bean显式的范围。

@Bean
public JobScope jobScope() {
    return new JobScope();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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