Springboot将@PersistenceContext EntityManager附加到@Bean

时间:2018-11-14 11:37:23

标签: java spring spring-boot jpa

我收到以下错误消息:

  

org.springframework.beans.factory.NoUniqueBeanDefinitionException:没有类型为'javax.persistence.EntityManagerFactory'的合格Bean:预期为单个匹配的Bean,但找到了4:D1EntityManager,D2EntityManager,BaseEntityManager,oldDbEntityManager

我在不同的文件中声明了此模板后面的4 EntityManagers

RepositoryConfigurationTemplate

@Configuration
@EnableJpaRepositories(
    basePackages = "...myReposPackages...",
    entityManagerFactoryRef = ConfigurationChanges.ENTITY_MANAGER_BEAN_NAME,
    transactionManagerRef = ConfigurationChanges.TRANSACTION_MANAGER_BEAN_NAME
)
@ComponentScan("...myScanPackages...")
@EnableConfigurationProperties
public class MyRepoConfigClass {
  ...
  ...
  public final static String ENTITY_MANAGER_BEAN_NAME = "...EntityManagerSingleConstant..."; // D1EntityManager / D2EntityManager / BaseEntityManager / oldDbEntityManager for different classes
  public final static String TRANSACTION_MANAGER_BEAN_NAME = "...TransactionManagerSingleConstant..."; // D1TransactionManager / D2TransactionManager / BaseTransactionManager / oldDbTransactionManager for different classes

  @Bean(name=ENTITY_MANAGER_BEAN_NAME)
  @Qualifier(ENTITY_MANAGER_BEAN_NAME) // Not sure if this is needed, I just added it trying to make it work
  public LocalContainerEntityManagerFactoryBean myClassEntityManager() {
    LocalContainerEntityManagerFactoryBean em
            = new LocalContainerEntityManagerFactoryBean();
    ... initializations ...
    em.setDataSource(...myDataSource...)
    em.setPackagesToScan("...EntitiesPackagesToScan...")

    em.setPersistenceUnitName(ENTITY_MANAGER_BEAN_NAME); // Not sure if this is needed, I just added it trying to make it work
  }

  @Bean(name=TRANSACTION_MANAGER_BEAN_NAME)
  @Qualifier(TRANSACTION_MANAGER_BEAN_NAME) // Not sure if this is needed, I just added it trying to make it work
  public PlatformTransactionManager myClassTransactionManager() {
    JpaTransactionManager transactionManager
            = new JpaTransactionManager();
    transactionManager.setEntityManagerFactory(
            myClassEntityManager().getObject());
    return transactionManager;
  }

}

我正在尝试像这样从Services引用它:

ServiceTemplate

@Service
public class myServiceClass {

  @PersistenceContext(unitName = MyRepoConfigClass.ENTITY_MANAGER_BEAN_NAME) // Thought this might be the solution for JPA PersistenceContext, but did not work anyway
  @Qualifier(MyRepoConfigClasss.ENTITY_MANAGER_BEAN_NAME) // Not sure if this is supposed to be done like this. In normal spring boot components it works
  private EntityManager entityManager; // <- This injection fails

}

从我的角度来看,我是通过EntityManger unitName )或@PersistenceContext

确切指定@Qualifier应该使用什么

但是,正如错误所言,springboot不知道4个Bean中的哪一个使用。

我想念什么吗?

更新

完整堆栈跟踪:

  

org.springframework.beans.factory.BeanCreationException:创建名称为“ changes”的bean时出错:持久性依赖项注入失败;嵌套的异常是org.springframework.beans.factory.NoUniqueBeanDefinitionException:没有类型为'javax.persistence.EntityManagerFactory'的合格bean:预期的单个匹配bean,但找到了5:D1EntityManager,D2EntityManager,BaseEntityManager,oldDbEntityManager,deviceApiGwDbConfigurationDbEntityManager       在org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.postProcessPropertyValues(PersistenceAnnotationBeanPostProcessor.java:356)〜[spring-orm-5.0.9.RELEASE.jar:5.0.9.RELEASE]       在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1383)〜[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE]       在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:575)〜[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE]       在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:498)〜[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE]       在org.springframework.beans.factory.support.AbstractBeanFactory.lambda $ doGetBean $ 0(AbstractBeanFactory.java:320)〜[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE]       在org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)〜[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE]       在org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318)〜[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE]       在org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)〜[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE]       在org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:846)〜[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE]       在org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:869)〜[spring-context-5.0.9.RELEASE.jar:5.0.9.RELEASE]       在org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550)〜[spring-context-5.0.9.RELEASE.jar:5.0.9.RELEASE]       在org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140)〜[spring-boot-2.0.5.RELEASE.jar:2.0.5.RELEASE]       在org.springframework.boot.SpringApplication.refresh(SpringApplication.java:780)上[spring-boot-2.0.5.RELEASE.jar:2.0.5.RELEASE]       在org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:412)上[spring-boot-2.0.5.RELEASE.jar:2.0.5.RELEASE]       在org.springframework.boot.SpringApplication.run(SpringApplication.java:333)上[spring-boot-2.0.5.RELEASE.jar:2.0.5.RELEASE]       在org.springframework.boot.SpringApplication.run(SpringApplication.java:1277)[spring-boot-2.0.5.RELEASE.jar:2.0.5.RELEASE]       在org.springframework.boot.SpringApplication.run(SpringApplication.java:1265)上[spring-boot-2.0.5.RELEASE.jar:2.0.5.RELEASE]       在com.mycompany.device.api.gw.Application.main(Application.java:35)[classes /:na]   由以下原因引起:org.springframework.beans.factory.NoUniqueBeanDefinitionException:没有类型为'javax.persistence.EntityManagerFactory'的合格bean:预期的单个匹配bean,但找到5:D1EntityManager,D2EntityManager,BaseEntityManager,oldDbEntityManager,deviceApiGwDbConfigurationDbEntityManager       在org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveNamedBean(DefaultListableBeanFactory.java:1139)〜[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE]       在org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveNamedBean(DefaultListableBeanFactory.java:1079)〜[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE]       在org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.findDefaultEntityManagerFactory(PersistenceAnnotationBeanPostProcessor.java:569)〜[spring-orm-5.0.9.RELEASE.jar:5.0.9.RELEASE]       在org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.findEntityManagerFactory(PersistenceAnnotationBeanPostProcessor.java:532)〜[spring-orm-5.0.9.RELEASE.jar:5.0.9.RELEASE]       在org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor $ PersistenceElement.resolveEntityManager(PersistenceAnnotationBeanPostProcessor.java:700)〜[spring-orm-5.0.9.RELEASE.jar:5.0.9.RELEASE]       在org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor $ PersistenceElement.getResourceToInject(PersistenceAnnotationBeanPostProcessor.java:673)〜[spring-orm-5.0.9.RELEASE.jar:5.0.9.RELEASE]       在org.springframework.beans.factory.annotation.InjectionMetadata $ InjectedElement.inject(InjectionMetadata.java:180)〜[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE]       在org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:90)〜[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE]       在org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.postProcessPropertyValues(PersistenceAnnotationBeanPostProcessor.java:353)〜[spring-orm-5.0.9.RELEASE.jar:5.0.9.RELEASE]       ...省略了17个常见框架

0 个答案:

没有答案