2016-05-12 102 views
0

我想用Spring Boot Hikari CP實現多個數據庫。我越來越春季啓動Hikari多個數據庫自動佈線失敗

Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [javax.sql.DataSource] is defined: expected single matching bean but found 2: hikari_primary,hikari_secondary 

供您參考我附上我的春天啓動數據源配置文件, 請不要去了小學和中學的命名規則(他們並不代表優先級),我的要求,它有兩個兩個連接池不同的數據庫

任何幫助理解

1.application.properties

spring.datasource.dataSourceClassName=com.microsoft.sqlserver.jdbc.SQLServerDataSource 


primary.spring.datasource.url=jdbc:sqlserver://xxx.xxx.xxx.xxx:1433;DatabaseName=training 
primary.spring.datasource.username=training 
primary.spring.datasource.password=training 
primary.spring.datasource.poolName=hikari_primary 
primary.spring.datasource.maximumPoolSize=5 
primary.spring.datasource.minimumIdle=3 
primary.spring.datasource.maxLifetime=2000000 
primary.spring.datasource.connectionTimeout=30000 
primary.spring.datasource.idleTimeout=30000 
primary.spring.datasource.pool-prepared-statements=true 
primary.spring.datasource.max-open-prepared-statements=250 

secondary.spring.datasource.url=jdbc:sqlserver://xxx.xxx.xxx.xxx:1433;DatabaseName=dev_xxxxx_core_v3 
secondary.spring.datasource.username=developer 
secondary.spring.datasource.password=Developer 
secondary.spring.datasource.poolName=hikari_secondary 
secondary.spring.datasource.maximumPoolSize=50 
secondary.spring.datasource.minimumIdle=30 
secondary.spring.datasource.maxLifetime=2000000 
secondary.spring.datasource.connectionTimeout=30000 
secondary.spring.datasource.idleTimeout=30000 
secondary.spring.datasource.pool-prepared-statements=true 
secondary.spring.datasource.max-open-prepared-statements=300 

2. PrimaryDataSourceConfig.java

@Configuration 
public class PrimaryDataSourceConfig { 

    @Value("${primary.spring.datasource.username}") 
    private String user; 

    @Value("${primary.spring.datasource.password}") 
    private String password; 

    @Value("${primary.spring.datasource.url}") 
    private String dataSourceUrl; 

    @Value("${spring.datasource.dataSourceClassName}") 
    private String dataSourceClassName; 

    @Value("${primary.spring.datasource.poolName}") 
    private String poolName; 

    @Value("${primary.spring.datasource.connectionTimeout}") 
    private int connectionTimeout; 

    @Value("${primary.spring.datasource.maxLifetime}") 
    private int maxLifetime; 

    @Value("${primary.spring.datasource.maximumPoolSize}") 
    private int maximumPoolSize; 

    @Value("${primary.spring.datasource.minimumIdle}") 
    private int minimumIdle; 

    @Value("${primary.spring.datasource.idleTimeout}") 
    private int idleTimeout; 

    @Bean(name="hikari_primary") 
    public HikariDataSource getHikariDataSourcePrimary() { 
     Properties dsProps = new Properties(); 
     dsProps.put("url", dataSourceUrl); 
     dsProps.put("user", user); 
     dsProps.put("password", password); 

     Properties configProps = new Properties(); 
     configProps.put("dataSourceClassName", dataSourceClassName); 
     configProps.put("poolName", poolName); 
     configProps.put("maximumPoolSize", maximumPoolSize); 
     configProps.put("minimumIdle", minimumIdle); 
     configProps.put("minimumIdle", minimumIdle); 
     configProps.put("connectionTimeout", connectionTimeout); 
     configProps.put("idleTimeout", idleTimeout); 
     configProps.put("dataSourceProperties", dsProps); 

     HikariConfig hc = new HikariConfig(configProps); 
     HikariDataSource ds = new HikariDataSource(hc); 
     return ds; 
    } 
} 

3. SecondayDataSourceConfig.java

@Configuration 
public class SecondaryDataSourceConfig { 


    @Value("${secondary.spring.datasource.username}") 
    private String user; 

    @Value("${secondary.spring.datasource.password}") 
    private String password; 

    @Value("${secondary.spring.datasource.url}") 
    private String dataSourceUrl; 

    @Value("${spring.datasource.dataSourceClassName}") 
    private String dataSourceClassName; 

    @Value("${secondary.spring.datasource.poolName}") 
    private String poolName; 

    @Value("${secondary.spring.datasource.connectionTimeout}") 
    private int connectionTimeout; 

    @Value("${secondary.spring.datasource.maxLifetime}") 
    private int maxLifetime; 

    @Value("${secondary.spring.datasource.maximumPoolSize}") 
    private int maximumPoolSize; 

    @Value("${secondary.spring.datasource.minimumIdle}") 
    private int minimumIdle; 

    @Value("${secondary.spring.datasource.idleTimeout}") 
    private int idleTimeout; 


    @Bean(name="hikari_secondary") 
    public HikariDataSource getHikariDataSourceSecondary() { 
     Properties dsProps = new Properties(); 
     dsProps.put("url", dataSourceUrl); 
     dsProps.put("user", user); 
     dsProps.put("password", password); 

     Properties configProps = new Properties(); 
     configProps.put("dataSourceClassName", dataSourceClassName); 
     configProps.put("poolName", poolName); 
     configProps.put("maximumPoolSize", maximumPoolSize); 
     configProps.put("minimumIdle", minimumIdle); 
     configProps.put("minimumIdle", minimumIdle); 
     configProps.put("connectionTimeout", connectionTimeout); 
     configProps.put("idleTimeout", idleTimeout); 
     configProps.put("dataSourceProperties", dsProps); 

     HikariConfig hc = new HikariConfig(configProps); 
     HikariDataSource ds = new HikariDataSource(hc); 
     return ds; 
    } 
} 

4. Application.java

@SpringBootApplication 
@ComponentScan("com.xxxx.springsql2o") 
@EnableAutoConfiguration 
public class Application 
{ 
    public static void main(String[] args) 
    { 
     SpringApplication.run(Application.class, args); 

    } 

    @Autowired 
    @Qualifier("hikari_primary") 
    DataSource hikariDataSourcePrimary; 

    @Autowired 
    @Qualifier("hikari_secondary") 
    DataSource hikariDataSourceSecondary; 


    @Bean(name= "primary_db") 
    public Sql2o getPrimarySql2o() 
    { 
     return new Sql2o(hikariDataSourcePrimary); 
    } 

    @Bean(name= "secondary_db") 
    public Sql2o getSecondarySql2o() 
    { 
     return new Sql2o(hikariDataSourceSecondary); 
    } 
} 

回答

0

春天開機自動配置通過@EnableAutoConfiguration您的應用程序(注意,這個註釋已經包含在由@SpringBootApplication註釋)。所以我的猜測是,你有一些依賴,春天試圖自動配置(例如JPA),它使用/需要DataSource。如果你能忍受這一點,你可以在你的DataSource Bean提供者方法上添加@Primary來滿足這種依賴關係。 所以,例如:

@Bean(name="hikari_primary") 
@Primary 
public HikariDataSource getHikariDataSourcePrimary() {... 

即使這應該工作,它會建議刪除自動配置例如JPA或其他任何彈簧引導都試圖自動配置,但您不需要/手動配置所有內容,因爲它適合您的應用程序需求。有兩個數據庫當然是一個自定義配置,並不符合彈簧引導易於使用的方法。