SpringBoot and SpringJDBC multiple datasources
<p>I am trying to use two datasources with my SpringBoot application and can't get the second datasource to autowire. I have tried many things but this is the closest I have come:</p>

<p>My Yaml file:</p>

<pre><code>spring:
  first-datasource:
    url: MyURLString1
    username: User
    password: Password
    driver-class-name: oracle.jdbc.OracleDriver
  second-datasource:
    url: MyURLString2
    username: User
    password: Password
    driver-class-name: oracle.jdbc.OracleDriver
</code></pre>

<p>My Application Class:</p>

<pre class="lang-java prettyprint-override"><code>@SpringBootApplication
public class MyApplication {

    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }

    @Bean
    @Primary
    @ConfigurationProperties(prefix = "spring.first-datasource")
    public DataSource firstDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean
    @ConfigurationProperties(prefix = "spring.second-datasource")
    public DataSource secondDataSource() {
        return DataSourceBuilder.create().build();
    }
}
</code></pre>

<p>And Finally my DAO:</p>

<pre class="lang-java prettyprint-override"><code>@Repository
public class MyDao {
    private static final String FIRST_SELECT = "select * from SomeTableInDB1";
    private static final String SECOND_SELECT = "select * from AnotherTableInDB2";

    @Autowired
    private JdbcTemplate firstJdbcTemplate;
    @Autowired
    @Qualifier("secondDataSource")
    private JdbcTemplate secondJdbcTemplate;

    List&lt;DB1Entity&gt; getDB1Entity(Long id) {
        return firstJdbcTemplate.query(FIRST_SELECT, new Object[] {id}, new BeanPropertyRowMapper(DB1Entity.class));
    }

    List&lt;DB2Entity&gt; getDB2Entity(Long id) {
        return secondJdbcTemplate.query(SECOND_SELECT, new Object[] {id}, new BeanPropertyRowMapper(DB2Entity.class));
    }
}
</code></pre>

<p>This is the closest I have come so far. I say it is closest because if I remove the @Qualifier then both of my dao methods actually work, assuming that the SECOND_SELECT statement is valid SQL for my DB1. Once I put in the @Qualifier for my non-primary datasouce then I get an autowire error because Spring is expecting a Datasouce object, not a JdbcTemplate object. That is weird to me as it does work with the primary datasource.</p>

<p>Here is my error:</p>

<p>Could not autowire field: private org.springframework.jdbc.core.JdbcTemplate org.my.classpath.secondJdbcTemplate; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.jdbc.core.JdbcTemplate] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=secondDataSource)}</p>