我有两个用于两个独立数据库的实体管理器配置,但是当我尝试自动连接实体管理器以配置我的 GraphQLExecutor bean 时,我收到一个异常,指出即使我指定了一个单元名称,仍有两个 bean 符合条件在 PersistenceContext 中。
例外
org.springframework.beans.factory.BeanCreationException:创建名为“graphQLExecutor”的bean时出错:资源依赖注入失败;嵌套异常是 org.springframework.beans.factory.NoUniqueBeanDefinitionException:没有可用的“javax.persistence.EntityManager”类型的合格 bean:预期的单个匹配 bean,但发现 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.org.RELEASE] springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1264) ~[spring-beans-4.3.9.
实体管理器 1
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() { …Run Code Online (Sandbox Code Playgroud)