我试图在spring批处理应用程序中配置两个数据源。一个用于批处理元数据表,另一个用于业务表。
my 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 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插入到一个新的配置类中。虽然它解决了最初的问题 `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 server示例。我查了一下,发现几年前不可能使用多个azure sql数据库,但基于这个答案,情况应该不再如此了。
1条答案
按热度按时间ghhaqwfi1#
这个问题很可能是因为
您应该在这里使用自动连线bean,而不是调用
batchDataSource()
我将springbatchconfig拆分为两个bean: