我想用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);
}
}