【发布时间】:2017-07-19 07:10:16
【问题描述】:
我在 1.5.1 版本中使用 spring boot 的应用程序出现问题。
我的应用程序需要与 2 个数据库(Oracle 和 MySQL)通信
我的应用程序使用 2 个数据源: - MySQL 数据源
@Configuration
public class OracleDBConfig {
@Bean(name = "oracleDb")
@ConfigurationProperties(prefix = "spring.ds_oracle")
public DataSource oracleDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "oracleJdbcTemplate")
public JdbcTemplate oracleJdbcTemplate(@Qualifier("oracleDb") DataSource dsOracle) {
return new JdbcTemplate(dsOracle);
}
}
-
Oracle 数据源
@配置 公共类 MySQLDBConfig {
@Bean(name = "mysqlDb") @ConfigurationProperties(prefix = "spring.ds_mysql") public DataSource mysqlDataSource() { return DataSourceBuilder.create().build(); } @Bean(name = "mysqlJdbcTemplate") public JdbcTemplate mySQLjdbcTemplate(@Qualifier("mysqlDb")DataSource dsMySQL) { return new JdbcTemplate(dsMySQL); } }
我已使用前缀在我的 applications.properties 中定义了 2 个数据源。
当我启动程序时出现此错误:
Parameter 0 of method oracleJdbcTemplate in com.bv.aircraft.config.OracleDBConfig required a single bean, but 2 were found:
- mysqlDb: defined by method 'mysqlDataSource' in class path resource [com/bv/aircraft/config/MySQLDBConfig.class]
- oracleDb: defined by method 'oracleDataSource' in class path resource [com/bv/aircraft/config/OracleDBConfig.class]
我尝试使用@Primary,但是当我需要使用其他数据源时它不起作用。
谢谢
【问题讨论】:
-
您遇到的错误是什么?
标签: spring oracle spring-boot datasource