2017-02-28 9 views
1

1.5.1バージョンのスプリングブートを使用するアプリケーションに問題があります。 - 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データソース

    @Configuration :

    私のアプリケーションは、2つのデータベース(OracleやMySQLの)

    私のアプリケーションの使用2つのデータソースと通信する必要がありますパブリッククラス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を使用しようとしましたが、私は他のデータソースを使用する必要がある場合、それは動作しません。

はどちらかあなた

+0

を? –

答えて

1

@EnableAutoConfiguration(exclude = {DataSourceTransactionManagerAutoConfiguration.class, DataSourceAutoConfiguration.class}) 

あなたの春のブート構成クラスの使用例次の行を追加します。あなたが取得しているエラーが何であるかを

@SpringBootApplication 
@EnableAutoConfiguration(exclude = {DataSourceTransactionManagerAutoConfiguration.class, DataSourceAutoConfiguration.class}) 
public class Application { 
    public static void main(String[] args) { 
     SpringApplication.run(Application.class, args); 
    } 
} 
+0

これは私の問題を解決します!ありがとうございました – Ahmed

+0

デフォルトでは、クラスパスからschema.sqlとdata.sqlを実行するようにDatasourceInitializerを初期化する、spring boot enable DatasourceAutoConfigurationが使用されています。これには、あなたのケースで欠けていた単一のデータソースが必要です。それを無効にする必要があります。 – mhshimul

0

ありがとう:

は、例えば、データソース構成からテンプレート構成を分離し、テンプレート構成に修飾子を持つ2つのデータソースに注入し、または単に直接作成するデータソースのメソッドを呼び出します

public JdbcTemplate oracleJdbcTemplate(oracleDataSource()) DataSource dsOracle) { 
    return new JdbcTemplate(dsOracle); 
} 
関連する問題