title: Spring Boot defining two datasources
body: <p>I have a Java 4 and Spring Boot 2.4.0-SNAPSHOT application.</p>
  <p>It needs to access two separate datasources. I am also using Spring jdbc to query the databases.</p>
  <p>I have tried an implementation (see below), but I get errors.</p>
  <p>I have the following:</p>
  <p><strong>application.properties</strong></p>
  <pre><code># pims datasource
  spring.datasource1.driver-class-name=org.postgresql.Driver
  spring.datasource1.url=jdbc:postgresql://localhost:5432/pims
  spring.datasource1.username=postgres
  spring.datasource1.password=
  spring.jpa.database-platform=postgres
  #spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
  spring.jpa.show-sql=false
  # approval datasource
  spring.datasource2.driver-class-name=org.postgresql.Driver
  spring.datasource2.url=jdbc:postgresql://localhost:5432/approval
  spring.datasource2.username=postgres
  spring.datasource2.password=
  </code></pre>
  <p>and</p>
  <p><strong>MultipleDBConfig.java</strong></p>
  <pre><code>@Configuration
  @ComponentScan(basePackages = &quot;com.nexct&quot;)
  public class MultipleDBConfig {
  
      @Bean(name = &quot;datasource1&quot;)
      @ConfigurationProperties(&quot;spring.datasource1&quot;)
      @Primary
      public DataSource dataSource1(){
          return DataSourceBuilder.create().build();
      }
  
      @Bean(name = &quot;datasource2&quot;)
      @ConfigurationProperties(&quot;spring.datasource2&quot;)
      public DataSource dataSource2(){
          return DataSourceBuilder.create().build();
      }
  }
  </code></pre>
  <p>Then in the DAO, I define the jdbcTemplate.</p>
  <p><strong>CompanyContactDAOImpl.java</strong></p>
  <pre><code>@Repository
  public class CompanyContactDAOImpl implements CompanyContactDAO {
  
      @Autowired
      @Qualifier(&quot;datasource1&quot;) // pims datasource
      private JdbcTemplate jdbcTemplate;
  </code></pre>
  <p><strong>ApprovalRequestDAOImpl.java</strong></p>
  <pre><code>@Repository
  public class ApprovalRequestDAOImpl implements ApprovalRequestDAO {
  
      @Autowired
      @Qualifier(&quot;datasource2&quot;) // approval datasource
      private JdbcTemplate jdbcTemplate;
  </code></pre>
  <p>Now when I start Spring Boot, I get the following error:</p>
  <blockquote>
  <p>Could not autowire. Qualified bean must be of 'JdbcTemplate' type.</p>
  </blockquote>
  <p>and</p>
  <blockquote>
  <p>Exception encountered during context initialization - cancelling
  refresh attempt:
  org.springframework.beans.factory.UnsatisfiedDependencyException:
  Error creating bean with name 'approvalRequestDAOImpl': Unsatisfied
  dependency expressed through field 'jdbcTemplate'; nested exception is
  org.springframework.beans.factory.NoSuchBeanDefinitionException: No
  qualifying bean of type 'org.springframework.jdbc.core.JdbcTemplate'
  available: expected at least 1 bean which qualifies as autowire
  candidate. Dependency annotations:
  {@org.springframework.beans.factory.annotation.Autowired(required=true),
  @org.springframework.beans.factory.annotation.Qualifier(&quot;datasource2&quot;)}</p>
  </blockquote>
  <p>and</p>
  <blockquote>
  <p>org.springframework.beans.factory.UnsatisfiedDependencyException:
  Error creating bean with name 'approvalRequestDAOImpl': Unsatisfied
  dependency expressed through field 'jdbcTemplate'; nested exception is
  org.springframework.beans.factory.NoSuchBeanDefinitionException: No
  qualifying bean of type 'org.springframework.jdbc.core.JdbcTemplate'
  available: expected at least 1 bean which qualifies as autowire
  candidate. Dependency annotations:
  {@org.springframework.beans.factory.annotation.Autowired(required=true),
  @org.springframework.beans.factory.annotation.Qualifier(&quot;datasource2&quot;)}
  at
  Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean
  of type 'org.springframework.jdbc.core.JdbcTemplate' available:
  expected at least 1 bean which qualifies as autowire candidate.
  Dependency annotations:
  {@org.springframework.beans.factory.annotation.Autowired(required=true),
  @org.springframework.beans.factory.annotation.Qualifier(&quot;datasource2&quot;)}</p>
  </blockquote>