gpt4 book ai didi

java - Spring Batch (modular=true) 创建作业配置 bean 两次

转载 作者:行者123 更新时间:2023-12-01 09:36:21 27 4
gpt4 key购买 nike

@Configuration
@EnableBatchProcessing(modular=true)
public class ModularJobConfiguration {

@Bean
public ApplicationContextFactory job() {
return new GenericApplicationContextFactory(FlatfileToDbJobConfiguration.class);
}

@Bean
public ApplicationContextFactory anotherJob() {
return new GenericApplicationContextFactory(FlatfileToDbJobAnotherConfiguration.class);
}

}

例如仅显示一个配置,另一个像这样。

@Configuration
public class FlatfileToDbJobConfiguration {

@Autowired
private JobBuilderFactory jobBuilders;

@Autowired
private StepBuilderFactory stepBuilders;

@Bean
public Job flatfileToDbJob(){
return jobBuilders.get("flatfileToDbJob")
.listener(protocolListener())
.start(step())
.build();
}

@Bean
public Step step(){
return stepBuilders.get("step")
.<Partner,Partner>chunk(1)
.reader(reader())
.processor(processor())
.writer(writer())
.build();
}

// ...
// rest part of code
// ...
}

一切正常,但配置文件中的所有 bean 方法都会调用两次。我认为,首先是在共同背景下,其次是在个人背景下。正常吗?我不能使用后置构造,因为它会调用两次。如何让 module=true 并且只有一个 bean 初始化?谢谢!

最佳答案

我找到了答案。我需要@Lazy初始化。然后配置将初始化,然后创建单独的上下文。

@Configuration
@Lazy
public class FlatfileToDbJobConfiguration {

@Autowired
private JobBuilderFactory jobBuilders;

@Autowired
private StepBuilderFactory stepBuilders;

@Bean
public Job flatfileToDbJob(){
return jobBuilders.get("flatfileToDbJob")
.listener(protocolListener())
.start(step())
.build();
}

@Bean
public Step step(){
return stepBuilders.get("step")
.<Partner,Partner>chunk(1)
.reader(reader())
.processor(processor())
.writer(writer())
.build();
}

// ...
// rest part of code
// ...
}

关于java - Spring Batch (modular=true) 创建作业配置 bean 两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38871402/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com