title: Spring boot @Qualifier doesn't work with datasources
body: <p>I'm building JPA configuration with multiple <em>persistence units</em> using different in-memory <em>datasources</em>, but the configuration fails resolving the qualified <em>datasource</em> for <em>entity manager factory bean</em> with the following error:</p>
  
  <pre><code>***************************
  APPLICATION FAILED TO START
  ***************************
  
  Description:
  
  Parameter 0 of method emfb in datasources.Application$PersistenceConfiguration required a single bean, but 2 were found:
          - ds1: defined by method 'ds1' in class path resource [datasources/Application$PersistenceConfiguration.class]
          - ds2: defined by method 'ds2' in class path resource [datasources/Application$PersistenceConfiguration.class]
  
  
  Action:
  
  Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed
  </code></pre>
  
  <p>Here is the sample application</p>
  
  <pre><code>package datasources;
  
  import javax.persistence.EntityManager;
  import javax.persistence.PersistenceContext;
  import javax.persistence.PersistenceContextType;
  import javax.sql.DataSource;
  import javax.ws.rs.ApplicationPath;
  import javax.ws.rs.GET;
  import javax.ws.rs.Path;
  import org.apache.log4j.Logger;
  import org.glassfish.jersey.server.ResourceConfig;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.beans.factory.annotation.Qualifier;
  import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
  import org.springframework.boot.autoconfigure.transaction.jta.JtaAutoConfiguration;
  import org.springframework.boot.builder.SpringApplicationBuilder;
  import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.ComponentScan;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.context.annotation.Primary;
  import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
  import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
  import org.springframework.stereotype.Component;
  
  @Configuration
  @EnableAutoConfiguration(exclude = {
  //      HibernateJpaAutoConfiguration.class,
  //      DataSourceAutoConfiguration.class
          JtaAutoConfiguration.class
  })
  @ComponentScan
  public class Application {
  
      public static void main(String[] args) {
  
          new SpringApplicationBuilder(Application.class)
              .build()
              .run(args);
      }
  
      @Component
      @Path("/ds")
      public static class DsApi {
  
          private final static Logger logger = Logger.getLogger(DsApi.class);
  
          @Autowired(required = false)
          @Qualifier("ds1")
          private DataSource ds;
  
          @GET
          public String ds() {
              logger.info("ds");
              return ds.toString();
          }
      }
  
      @Component
      @Path("/em")
      public static class EmApi {
  
          private final static Logger logger = Logger.getLogger(EmApi.class);
  
          @PersistenceContext(unitName = "ds2", type = PersistenceContextType.TRANSACTION)
          private EntityManager em;
  
          @GET
          public String em() {
              logger.info("em");
              return em.toString();
          }
      }
  
      @Configuration
      @ApplicationPath("/jersey")
      public static class JerseyConfig extends ResourceConfig {
          public JerseyConfig() {
              register(DsApi.class);
              register(EmApi.class);
          }
      }
  
      @Configuration
      public static class PersistenceConfiguration {
  
          @Bean
          @Qualifier("ds1")
          public DataSource ds1() {
              return new EmbeddedDatabaseBuilder().build();
          }
  
          @Bean
          @Qualifier("ds2")
          public DataSource ds2() {
              return new EmbeddedDatabaseBuilder().build();
          }
  
          @Bean
          @Primary
          @Autowired
          public LocalContainerEntityManagerFactoryBean emfb(@Qualifier("ds1") DataSource ds, EntityManagerFactoryBuilder emfb) {
              return emfb.dataSource(ds)
                      .packages(Application.class)
                      .persistenceUnit("ds1")
                      .build();
          }
  
          @Bean
          @Autowired
          public LocalContainerEntityManagerFactoryBean emfb2(@Qualifier("ds2") DataSource ds, EntityManagerFactoryBuilder emfb) {
              return emfb.dataSource(ds)
                      .packages(Application.class)
                      .persistenceUnit("ds2")
                      .build();
          }
      }
  }
  </code></pre>