title: No qualifying bean of type 'javax.persistence.EntityManager' available: expected single matching bean but found 2
body: <p>I have two entity manager configurations for two separate databases but when I try to auto-wire an entity manager in to configure my GraphQLExecutor bean I get an exception stating that there are two beans that match the criteria even though I've specified a unit name in the PersistenceContext.</p>
  
  <p><strong>Exception</strong></p>
  
  <blockquote>
    <p>org.springframework.beans.factory.BeanCreationException: Error
    creating bean with name 'graphQLExecutor': Injection of resource
    dependencies failed; nested exception is
    org.springframework.beans.factory.NoUniqueBeanDefinitionException: No
    qualifying bean of type 'javax.persistence.EntityManager' available:
    expected single matching bean but found 2:
    org.springframework.orm.jpa.SharedEntityManagerCreator#0,org.springframework.orm.jpa.SharedEntityManagerCreator#1
    at
    org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:321)
    ~[spring-context-4.3.9.RELEASE.jar:4.3.9.RELEASE] at
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1264)
    ~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE] at
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
    ~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE] at
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
    ~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE] at
    org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    ~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE] at
    org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    ~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE] at
    org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    ~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE] at
    org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    ~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE] at
    org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761)
    ~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE] at
    org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867)
    ~[spring-context-4.3.9.RELEASE.jar:4.3.9.RELEASE] at
    org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543)
    ~[spring-context-4.3.9.RELEASE.jar:4.3.9.RELEASE] at
    org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122)
    ~[spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE] at
    org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693)
    [spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE] at
    org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360)
    [spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE] at
    org.springframework.boot.SpringApplication.run(SpringApplication.java:303)
    [spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE] at
    org.springframework.boot.SpringApplication.run(SpringApplication.java:1118)
    [spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE] at
    org.springframework.boot.SpringApplication.run(SpringApplication.java:1107)
    [spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE] at
    com.ogl.JpaDemoApplication.main(JpaDemoApplication.java:15)
    [classes/:na] Caused by:
    org.springframework.beans.factory.NoUniqueBeanDefinitionException: No
    qualifying bean of type 'javax.persistence.EntityManager' available:
    expected single matching bean but found 2:
    org.springframework.orm.jpa.SharedEntityManagerCreator#0,org.springframework.orm.jpa.SharedEntityManagerCreator#1
      at
    org.springframework.beans.factory.config.DependencyDescriptor.resolveNotUnique(DependencyDescriptor.java:173)
    ~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE]   at
    org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1116)
    ~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE]   at
    org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066)
    ~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE]   at
    org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.autowireResource(CommonAnnotationBeanPostProcessor.java:518)
    ~[spring-context-4.3.9.RELEASE.jar:4.3.9.RELEASE]     at
    org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.getResource(CommonAnnotationBeanPostProcessor.java:496)
    ~[spring-context-4.3.9.RELEASE.jar:4.3.9.RELEASE]     at
    org.springframework.context.annotation.CommonAnnotationBeanPostProcessor$ResourceElement.getResourceToInject(CommonAnnotationBeanPostProcessor.java:627)
    ~[spring-context-4.3.9.RELEASE.jar:4.3.9.RELEASE]     at
    org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.java:169)
    ~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE]   at
    org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
    ~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE]   at
    org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:318)
    ~[spring-context-4.3.9.RELEASE.jar:4.3.9.RELEASE]     ... 17 common
    frames omitted</p>
  </blockquote>
  
  <p><strong>Entity Manager 1</strong></p>
  
  <pre><code>package com.ogl;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.core.env.Environment;
  import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
  import org.springframework.jdbc.datasource.DriverManagerDataSource;
  import org.springframework.orm.jpa.JpaTransactionManager;
  import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
  import org.springframework.orm.jpa.vendor.Database;
  import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
  import org.springframework.transaction.PlatformTransactionManager;
  
  import javax.sql.DataSource;
  
  @Configuration
  @EnableJpaRepositories(basePackages = "com.ogl.system", entityManagerFactoryRef = "companyEntityManagerFactory", transactionManagerRef = "companyTransactionManager")
  public class SystemJpaConfig {
  
    private final Environment environment;
  
    @Autowired
    public SystemJpaConfig(Environment environment) {
      this.environment = environment;
    }
  
    @Bean("systemEntityManagerFactory")
    public LocalContainerEntityManagerFactoryBean systemEntityManagerFactory() {
      LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
      entityManagerFactoryBean.setPackagesToScan("com.ogl.system");
      entityManagerFactoryBean.setPersistenceUnitName("system");
      entityManagerFactoryBean.setDataSource(systemDataSource());
  
      HibernateJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
      adapter.setDatabase(Database.POSTGRESQL);
      adapter.setShowSql(true);
      adapter.setGenerateDdl(false);
  
      entityManagerFactoryBean.setJpaVendorAdapter(adapter);
  
      return entityManagerFactoryBean;
    }
  
    @Bean
    DataSource systemDataSource() {
      DriverManagerDataSource dataSource = new DriverManagerDataSource();
      dataSource.setDriverClassName(environment.getProperty("p4.datasource.driver"));
      dataSource.setUrl(environment.getProperty("p4.system.url"));
      dataSource.setUsername(environment.getProperty("p4.system.user"));
      dataSource.setPassword(environment.getProperty("p4.system.password"));
  
      return dataSource;
    }
  
    @Bean
    public PlatformTransactionManager systemTransactionManager() {
      JpaTransactionManager transactionManager = new JpaTransactionManager();
      transactionManager.setEntityManagerFactory((systemEntityManagerFactory().getObject()));
  
      return transactionManager;
    }
  }
  </code></pre>
  
  <p><strong>Entity Manager 2</strong></p>
  
  <pre><code>package com.ogl;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.core.env.Environment;
  import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
  import org.springframework.jdbc.datasource.DriverManagerDataSource;
  import org.springframework.orm.jpa.JpaTransactionManager;
  import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
  import org.springframework.orm.jpa.vendor.Database;
  import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
  import org.springframework.transaction.PlatformTransactionManager;
  
  import javax.sql.DataSource;
  
  @Configuration
  @EnableJpaRepositories(basePackages = "com.ogl.company", entityManagerFactoryRef = "companyEntityManagerFactory", transactionManagerRef = "companyTransactionManager")
  public class CompanyJpaConfig {
  
    private final Environment environment;
  
    @Autowired
    public CompanyJpaConfig(Environment environment) {
      this.environment = environment;
    }
  
    @Primary
    @Bean("companyEntityManagerFactory")
    public LocalContainerEntityManagerFactoryBean companyEntityManagerFactory() {
      LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
      entityManagerFactoryBean.setPackagesToScan("com.ogl.company");
      entityManagerFactoryBean.setPersistenceUnitName("company");
      entityManagerFactoryBean.setDataSource(companyDataSource());
  
      HibernateJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
      adapter.setDatabase(Database.POSTGRESQL);
      adapter.setShowSql(true);
      adapter.setGenerateDdl(false);
  
      entityManagerFactoryBean.setJpaVendorAdapter(adapter);
  
      return entityManagerFactoryBean;
    }
  
    @Bean
    DataSource companyDataSource() {
      DriverManagerDataSource dataSource = new DriverManagerDataSource();
      dataSource.setDriverClassName(environment.getProperty("p4.datasource.driver"));
      dataSource.setUrl(environment.getProperty("p4.company.url"));
      dataSource.setUsername(environment.getProperty("p4.company.user"));
      dataSource.setPassword(environment.getProperty("p4.company.password"));
  
      return dataSource;
    }
  
    @Primary
    @Bean
    public PlatformTransactionManager companyTransactionManager() {
      JpaTransactionManager transactionManager = new JpaTransactionManager();
      transactionManager.setEntityManagerFactory((companyEntityManagerFactory().getObject()));
  
      return transactionManager;
    }
  }
  </code></pre>
  
  <p><strong>Injection</strong></p>
  
  <pre><code>package com.ogl;
  
  import org.crygier.graphql.GraphQLExecutor;
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.ComponentScan;
  import org.springframework.context.annotation.Configuration;
  
  import javax.persistence.EntityManager;
  import javax.persistence.PersistenceContext;
  
  @Configuration
  @ComponentScan
  public class GraphQLJpaConfig {
  
    @PersistenceContext(unitName = "company")
    private EntityManager entityManager;
  
    @Bean
    public GraphQLExecutor graphQLExecutor() {
      return new GraphQLExecutor(entityManager);
    }
  }
  </code></pre>
  
  <p><strong>pom.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"&gt;
      &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
  
      &lt;groupId&gt;com.ogl&lt;/groupId&gt;
      &lt;artifactId&gt;jpa-demo&lt;/artifactId&gt;
      &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
      &lt;packaging&gt;jar&lt;/packaging&gt;
  
      &lt;name&gt;jpa-demo&lt;/name&gt;
      &lt;description&gt;Demo project for JPA&lt;/description&gt;
  
      &lt;parent&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
          &lt;version&gt;1.5.4.RELEASE&lt;/version&gt;
          &lt;relativePath/&gt; &lt;!-- lookup parent from repository --&gt;
      &lt;/parent&gt;
  
      &lt;properties&gt;
          &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
          &lt;project.reporting.outputEncoding&gt;UTF-8&lt;/project.reporting.outputEncoding&gt;
          &lt;java.version&gt;1.8&lt;/java.version&gt;
      &lt;/properties&gt;
  
      &lt;dependencies&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-data-jpa&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.postgresql&lt;/groupId&gt;
              &lt;artifactId&gt;postgresql&lt;/artifactId&gt;
              &lt;scope&gt;runtime&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;com.graphql-java&lt;/groupId&gt;
              &lt;artifactId&gt;graphql-java&lt;/artifactId&gt;
              &lt;version&gt;2.3.0&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;com.crygier&lt;/groupId&gt;
              &lt;artifactId&gt;graphql-jpa&lt;/artifactId&gt;
              &lt;version&gt;0.3&lt;/version&gt;
          &lt;/dependency&gt;
      &lt;/dependencies&gt;
  
      &lt;build&gt;
          &lt;plugins&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
                  &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
              &lt;/plugin&gt;
          &lt;/plugins&gt;
      &lt;/build&gt;
  &lt;/project&gt;
  </code></pre>