Stack Exchange
Spring Boot Hikari Multiple Database Autowiring Failing
I am trying to implement multiple database with Spring Boot Hikari CP. I am getting
   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

For your reference I am attaching my spring boot datasource configuration files,
please dont go over primary and secondary naming conventions(they dont represent priorities), my requirement it to have two connection pool for two different databases.
Any help is appreciated
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);
    }
}


Spring boot is auto-configuring your application via @EnableAutoConfiguration (note that this annotation is already included in the composed @SpringBootApplication annotation). So my guess would be that you have some dependency that spring is trying to auto-configure (e.g. JPA) which uses/needs DataSource. If you can live with this, you can add @Primary to on of your DataSource Bean provider methods in order to satisfy that dependency.
So, for instance:
@Bean(name="hikari_primary")
@Primary
public HikariDataSource getHikariDataSourcePrimary() {...

Even if this should work, it would be recommended to remove auto-configuration for e.g. JPA or whatever spring boot is trying to auto-configure but you don't use/need and configure everything manually as it suits your application needs. Have two databases is certainly a custom configuration and does not conform to the spring boot easy-out-of-the-box approach.  
