如何具有多个spring boot batch配置类

时间:2019-11-16 10:36:29

标签: java spring-batch

如果我想在批处理项目中有几个工作。每个文件仅包含一个作业。

工作1:

@Configuration
public class StartTest1 {

    @Autowired
    JobBuilderFactory jobBuilderFactory;

    @Autowired
    StepBuilderFactory stepBuilderFactory;

    @Bean
    public Job processJob() {
        return jobBuilderFactory.get("processJob1").incrementer(new RunIdIncrementer())....;
    }
    ....
}

工作2:

@Configuration
public class StartTest2 {

    @Autowired
    JobBuilderFactory jobBuilderFactory;

    @Autowired
    StepBuilderFactory stepBuilderFactory;

    @Bean
    public Job processJob() {
        return jobBuilderFactory.get("processJob2").incrementer(new RunIdIncrementer())....;
    }
    ....
}

我尝试为每个类设置@Configuration批注。但是,只有一个作业在运行时执行。如何拥有多个作业文件?

2 个答案:

答案 0 :(得分:1)

我认为问题在于两个作业bean都具有相同的名称processJob,因此在构建应用程序上下文时,一个覆盖另一个。最简单的解决方案是使用唯一的Bean名称,例如您为Job指定的名称:processJob1processJob2

答案 1 :(得分:0)

我找到了另一种方式,

  

设置不同的bean名称!

工作1:

@Configuration
public class StartTest1 {

    @Autowired
    JobBuilderFactory jobBuilderFactory;

    @Autowired
    StepBuilderFactory stepBuilderFactory;

    @Bean("job1")
    public Job processJob() {
        return jobBuilderFactory.get("processJob1").incrementer(new RunIdIncrementer())....;
    }
    ....
}

工作2:

@Configuration
public class StartTest2 {

    @Autowired
    JobBuilderFactory jobBuilderFactory;

    @Autowired
    StepBuilderFactory stepBuilderFactory;

    @Bean("job2")
    public Job processJob() {
        return jobBuilderFactory.get("processJob2").incrementer(new RunIdIncrementer())....;
    }
    ....
}