Ich versuche, mehrere Datenbanken mit Spring Boot Hikari CP zu implementieren. Ich binSpring Boot Hikari Mehrere Datenbank Autowiring Failing
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
Zu Ihrer Information bekomme ich meine Feder-Boot-Datenquelle Konfigurationsdateien bin Befestigung, gehen Sie bitte über die primären und sekundären Namenskonventionen nicht (sie nicht Prioritäten darstellen), es ist meine Anforderung zwei Verbindungspool haben für zwei verschiedene Datenbanken.
Jede Hilfe
1.application.properties geschätzt
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);
}
}