在 spring 批处理中配置多个数据源时出现无法解决的循环引用错误

Unresolveable Circular reference error when configuring multiple datasources in a spring batch

我正在尝试在我的 spring 批处理应用程序中配置两个数据源。一个用于批量元数据表,另一个用于业务表。

来自我的 application.properties 文件的片段:

spring.datasource.url=
spring.datasource.username=
spring.datasource.password=
spring.datasource.driver-class-name=

spring.batchdatasource.url=
spring.batchdatasource.username=
spring.batchdatasource.password=
spring.batchdatasource.driver-class-name=

我的批处理配置文件:

@Configuration
public class SpringBatchConfig extends DefaultBatchConfigurer{

    @Autowired
    private JobBuilderFactory jobs;

    @Autowired
    private StepBuilderFactory steps;

//  @Autowired
//  private DataSource dataSource;

    @Autowired
    private PlatformTransactionManager transactionManager;


    @Bean(name = "batchDatasource")
    @ConfigurationProperties(prefix="spring.batchdatasource")
    public DataSource batchDataSource(){
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "primaryDatasource")
    @ConfigurationProperties(prefix="spring.datasource")
    @Primary
    public DataSource primaryDataSource() {
        return DataSourceBuilder.create().build();
    }


    @Override
    public JobRepository createJobRepository() throws Exception {
    JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean();
    factory.setDataSource(batchDataSource());
//  factory.setDataSource(dataSource);
    factory.setTransactionManager(transactionManager);
    factory.setTablePrefix("schema1"+ ".BATCH_");
    factory.afterPropertiesSet();
    return factory.getObject();
    }

   /* Job and step bean definitions here */

我的主class是注释为@EnableBatchProcessing

的那个
@SpringBootApplication
@EnableBatchProcessing
public class SpringBatchExample1Application {

    public static void main(String[] args) {
         SpringApplication.run(SampleApplication.class, args);
    }
}

我在尝试配置两个数据源时收到此 Requested bean is currently in creation: Is there an unresolvable circular reference?。当通过自动装配(参考注释掉的代码行)而不是创建多个 bean 使用单个数据源时,它工作正常。 以下是异常片段:

Error creating bean with name 'springBatchConfig': Unsatisfied dependency expressed through method 'setDataSource' parameter 0; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'batchDatasource': Requested bean is currently in creation: Is there an unresolvable circular reference?

我查了一下,发现当依赖于仍未创建或正在创建的 bean 时会发生这种情况。我只是在插入数据源的 createJobRepository 方法中看到它。即使我没有 createJobRepository 方法,我仍然会遇到错误。

似乎要求数据源 bean 在其他 bean 之前创建。我尝试使用 @Order 注释,但没有成功。

编辑:

我尝试了下面@Mykhailo Skliar 接受的答案中的解决方案,并将数据源 bean 分离到一个新的配置中 class。虽然它解决了最初的 Unresolveble circular reference 问题,但它导致我出现以下错误:

Error creating bean with name 'springBatchConfig': Invocation of init method failed; nested exception is org.springframework.batch.core.configuration.BatchConfigurationException: java.lang.IllegalArgumentException: jdbcUrl is required with driverClassName.

基于,我将我的url属性名字改成了如下:

spring.datasource.jdbc-url=

spring.datasource.jdbc-url=

虽然它解决了 jdbcUrl 错误,但它带来了另一个问题:

Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: Reference to database and/or server name in 'sample-sql-server.schema1.MY_TABLE_NAME' is not supported in this version of SQL Server.

我的两个数据源都是 Azure SQL 服务器实例。 我查了一下,发现几年前是不能使用多个 Azure SQL 数据库的,但是基于 this answer 应该不会再这样了。

这个问题很可能是因为

factory.setDataSource(batchDataSource());

你应该在这里使用自动装配的 bean,而不是调用 batchDataSource()

我会将 SpringBatchConfig 分成两个 bean:

@Configuration
public class DataSourceConfig {



    @Bean(name = "batchDatasource")
    @ConfigurationProperties(prefix="spring.batchdatasource")
    public DataSource batchDataSource(){
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "primaryDatasource")
    @ConfigurationProperties(prefix="spring.datasource")
    @Primary
    public DataSource primaryDataSource() {
        return DataSourceBuilder.create().build();
    }
}






@Configuration
public class SpringBatchConfig extends DefaultBatchConfigurer{

    @Autowired
    private JobBuilderFactory jobs;

    @Autowired
    private StepBuilderFactory steps;


    @Qualifier("batchDataSource")
    @Autowired
    private DataSource batchDataSource;

    @Autowired
    private PlatformTransactionManager transactionManager;



    @Override
    public JobRepository createJobRepository() throws Exception {
    JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean();
    factory.setDataSource(batchDataSource);
    factory.setTransactionManager(transactionManager);
    factory.setTablePrefix("schema1"+ ".BATCH_");
    factory.afterPropertiesSet();
    return factory.getObject();
    }
}