Spring 启动 - 多个数据源的问题
Spring boot - Issue with multiple DataSource
我有一个 ReST 服务需要从两个不同的数据库(Oracle 和 MySQL)获取数据并将这些数据合并到响应中。
我有以下配置。
数据库 1 的配置:
@Configuration
public class DbConfig_DB1{
@Bean(name="siebelDataSource")
public EmbeddedDatabase siebelDataSource(){
return new EmbeddedDatabaseBuilder().
setType(EmbeddedDatabaseType.H2).
addScript("schema.sql").
addScript("test-data.sql").
build();
}
@Autowired
@Qualifier("siebelDataSource")
@Bean(name = "siebelJdbcTemplate")
public JdbcTemplate siebelJdbcTemplate(DataSource siebelDataSource) {
return new JdbcTemplate(siebelDataSource);
}
}
DB2 的配置:
@Configuration
public class DbConfig_DB2{
@Bean(name="brmDataSource")
public EmbeddedDatabase brmDataSource(){
return new EmbeddedDatabaseBuilder().
setType(EmbeddedDatabaseType.H2).
addScript("schema-1.sql").
addScript("test-data-1.sql").
build();
}
@Autowired
@Qualifier("brmDataSource")
@Bean(name = "brmJdbcTemplate")
public JdbcTemplate brmJdbcTemplate(DataSource brmDataSource) {
return new JdbcTemplate(brmDataSource);
}
}
数据访问:
@Repository
public class SiebelDataAccess {
protected final Logger log = LoggerFactory.getLogger(getClass());
@Autowired
@Qualifier("siebelJdbcTemplate")
protected JdbcTemplate jdbc;
public String getEmpName(Integer id) {
System.out.println(jdbc.queryForObject("select count(*) from employee", Integer.class));
Object[] parameters = new Object[] { id };
String name = jdbc.queryForObject(
"select name from employee where id = ?", parameters,
String.class);
return name;
}
}
我无法启动应用程序,因为出现以下错误:
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private javax.sql.DataSource org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration.dataSource;
nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [javax.sql.DataSource] is defined: expected single matching bean but found 2: brmDataSource,siebelDataSource
问题出在上下文中的两个 DataSource bean 上。如何解决?
您可以将其中之一标记为 @Primary
,这样 Spring 事务管理器的启动自动配置就会知道选择哪一个。如果您需要管理与它们两者的交易,那么恐怕您必须明确设置交易管理。
请参考Spring引导documentation
Creating more than one data source works the same as creating the first one. You might want to mark one of them as @Primary if you are using the default auto-configuration for JDBC or JPA (then that one will be picked up by any @Autowired injections).
我有一个 ReST 服务需要从两个不同的数据库(Oracle 和 MySQL)获取数据并将这些数据合并到响应中。
我有以下配置。
数据库 1 的配置:
@Configuration
public class DbConfig_DB1{
@Bean(name="siebelDataSource")
public EmbeddedDatabase siebelDataSource(){
return new EmbeddedDatabaseBuilder().
setType(EmbeddedDatabaseType.H2).
addScript("schema.sql").
addScript("test-data.sql").
build();
}
@Autowired
@Qualifier("siebelDataSource")
@Bean(name = "siebelJdbcTemplate")
public JdbcTemplate siebelJdbcTemplate(DataSource siebelDataSource) {
return new JdbcTemplate(siebelDataSource);
}
}
DB2 的配置:
@Configuration
public class DbConfig_DB2{
@Bean(name="brmDataSource")
public EmbeddedDatabase brmDataSource(){
return new EmbeddedDatabaseBuilder().
setType(EmbeddedDatabaseType.H2).
addScript("schema-1.sql").
addScript("test-data-1.sql").
build();
}
@Autowired
@Qualifier("brmDataSource")
@Bean(name = "brmJdbcTemplate")
public JdbcTemplate brmJdbcTemplate(DataSource brmDataSource) {
return new JdbcTemplate(brmDataSource);
}
}
数据访问:
@Repository
public class SiebelDataAccess {
protected final Logger log = LoggerFactory.getLogger(getClass());
@Autowired
@Qualifier("siebelJdbcTemplate")
protected JdbcTemplate jdbc;
public String getEmpName(Integer id) {
System.out.println(jdbc.queryForObject("select count(*) from employee", Integer.class));
Object[] parameters = new Object[] { id };
String name = jdbc.queryForObject(
"select name from employee where id = ?", parameters,
String.class);
return name;
}
}
我无法启动应用程序,因为出现以下错误:
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private javax.sql.DataSource org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration.dataSource;
nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [javax.sql.DataSource] is defined: expected single matching bean but found 2: brmDataSource,siebelDataSource
问题出在上下文中的两个 DataSource bean 上。如何解决?
您可以将其中之一标记为 @Primary
,这样 Spring 事务管理器的启动自动配置就会知道选择哪一个。如果您需要管理与它们两者的交易,那么恐怕您必须明确设置交易管理。
请参考Spring引导documentation
Creating more than one data source works the same as creating the first one. You might want to mark one of them as @Primary if you are using the default auto-configuration for JDBC or JPA (then that one will be picked up by any @Autowired injections).