我正在尝试在我的 Spring Batch 应用程序中配置两个数据源。一个用于批量元数据表,另一个用于业务表。
我的 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 */
我的主类是用@EnableBatchProcessing
注释的类
@SpringBootApplication
@EnableBatchProcessing
public class SpringBatchExample1Application {
public static void main(String[] args) {
SpringApplication.run(SampleApplication.class, args);
}
}
当尝试配置两个数据源时,我收到此Requested bean is current in Creation: Is There an unresolvable Circle Reference?
。当通过 Autowiring 使用单个数据源(引用注释掉的代码行)而不是创建多个 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 分离到一个新的配置类中。虽然它不再解决最初的无法解析的循环引用
问题,但它导致我出现以下错误:
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.
基于this answer ,我更改了 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());
您应该在此处使用 Autowiring 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();
}
}
关于java - 在 Spring Batch 中配置多个数据源时出现无法解决的循环引用错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68293123/