Spring batch – 一步使用两个数据源
Spring batch – using two data source in one step
目前我的 spring-batch-app 配置为仅使用一个数据源(属性文件)。当 运行 应用程序 spring 将选择默认配置。
spring.datasource.url=jdbc:sqlserver ...
spring.datasource.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.jpa.show-sql=true
spring.jpa.generate-ddl=false
spring.jpa.hibernate.ddl-auto=none
spring.jpa.hibernate.naming.physical-
strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.SQLServer2012Dialect
我有一个任务要创建一个需要来自另一个数据库的一些数据的作业。基本上,该步骤将从一个数据源检索数据并将数据写入另一个数据源。
我为新数据源创建了一个 bean:
@Bean
public DataSource melDataSource() {
DriverManagerDataSource melDataSource = new DriverManagerDataSource();
melDataSource.setDriverClassName("prestosql....");
melDataSource.setUrl("....");
melDataSource.setUsername("....");
melDataSource.setPassword("....");
return melDataSource;
}
这就是我调用数据源的方式:
@Autowired
private DataSource dataSource;
@Autowired
private DataSource melDataSource;
当运行程序出现以下错误:
Error creating bean with name
'org.springframework.batch.core.configuration.annotation.SimpleBatchConfiguration': Unsatisfied
dependency expressed through field 'dataSource'; nested exception is
org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name
'melDataSource': Requested bean is currently in creation: Is there an unresolvable circular
reference?
如何添加另一个数据源?
谢谢
您需要将数据源 bean 声明移动到单独的 class 中,并在批处理配置 class 中导入该 class。类似于:
class DataSourceConfiguration {
@Bean
public DataSource melDataSource() {
DriverManagerDataSource melDataSource = new DriverManagerDataSource();
melDataSource.setDriverClassName("prestosql....");
melDataSource.setUrl("....");
melDataSource.setUsername("....");
melDataSource.setPassword("....");
return melDataSource;
}
}
@Configuration
@Import(DataSourceConfiguration.class)
class BatchConfiguration {
// use datasource bean here
}
目前我的 spring-batch-app 配置为仅使用一个数据源(属性文件)。当 运行 应用程序 spring 将选择默认配置。
spring.datasource.url=jdbc:sqlserver ...
spring.datasource.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.jpa.show-sql=true
spring.jpa.generate-ddl=false
spring.jpa.hibernate.ddl-auto=none
spring.jpa.hibernate.naming.physical-
strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.SQLServer2012Dialect
我有一个任务要创建一个需要来自另一个数据库的一些数据的作业。基本上,该步骤将从一个数据源检索数据并将数据写入另一个数据源。
我为新数据源创建了一个 bean:
@Bean
public DataSource melDataSource() {
DriverManagerDataSource melDataSource = new DriverManagerDataSource();
melDataSource.setDriverClassName("prestosql....");
melDataSource.setUrl("....");
melDataSource.setUsername("....");
melDataSource.setPassword("....");
return melDataSource;
}
这就是我调用数据源的方式:
@Autowired
private DataSource dataSource;
@Autowired
private DataSource melDataSource;
当运行程序出现以下错误:
Error creating bean with name
'org.springframework.batch.core.configuration.annotation.SimpleBatchConfiguration': Unsatisfied
dependency expressed through field 'dataSource'; nested exception is
org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name
'melDataSource': Requested bean is currently in creation: Is there an unresolvable circular
reference?
如何添加另一个数据源?
谢谢
您需要将数据源 bean 声明移动到单独的 class 中,并在批处理配置 class 中导入该 class。类似于:
class DataSourceConfiguration {
@Bean
public DataSource melDataSource() {
DriverManagerDataSource melDataSource = new DriverManagerDataSource();
melDataSource.setDriverClassName("prestosql....");
melDataSource.setUrl("....");
melDataSource.setUsername("....");
melDataSource.setPassword("....");
return melDataSource;
}
}
@Configuration
@Import(DataSourceConfiguration.class)
class BatchConfiguration {
// use datasource bean here
}