小编典典

在Spring Batch中使用多个数据源

spring

我有两个名为simple-core-impl和的项目simple-core-web。

这两个项目都是,spring based并且都具有一个父项目名称simple-core。

我simple-impl-config.xml在simple-core-impl项目simple-web-config.xml中simple-impl-config.xml。

我有一个具有类的bean:simple service它具有一个向我返回消息“ hello World”的方法。

我想在中导入simple-impl-config.xml,simple-web-config.xml以便Bean可以在simple-core-web项目中的控制器中使用。

simple-core-web项目中有一个jar simple-core-impl项目。

因此,请告诉我如何将spring-config.xml一个项目导入spring-config.xml另一个项目,以便只需导入即可将first的所有bean导入另一个项目?

我不想重写所有bean。我正在尝试在Spring Batch中配置几个数据源。在启动时,Spring Batch抛出以下异常:

To use the default BatchConfigurer the context must contain no more thanone DataSource, found 2

批处理配置中的代码段

@Configuration
@EnableBatchProcessing 
public class BatchJobConfiguration {

    @Primary
    @Bean(name = "baseDatasource")
    public DataSource dataSource() {
         // first datasource definition here
    }
    @Bean(name = "secondaryDataSource")
    public DataSource dataSource2() {
         // second datasource definition here
    }
    ...
}

不知道为什么会看到此异常,因为我已经看到了Spring批处理的一些基于xml的配置,这些配置声明了多个数据源。我正在使用Spring Batch核心版本3.0.1.RELEASE和Spring Boot版本1.1.5.RELEASE。任何帮助将不胜感激。


阅读 5793

收藏
2020-04-12

共1个答案

小编典典

你必须提供自己的BatchConfigurer。Spring不想为你做出决定

@Configuration
@EnableBatchProcessing
public class BatchConfig {

     @Bean
      BatchConfigurer configurer(@Qualifier("batchDataSource") DataSource dataSource){
        return new DefaultBatchConfigurer(dataSource);
      }

...
2020-04-12