没有定义[javax.persistence.EntityManagerFactory]类型的唯一bean:期望的单个bean但找到了2

时间:2011-07-14 15:37:15

标签: hibernate spring jpa

我为两个不同的数据库定义了两个EntityManager bean。每个EntityManager bean都引用<persistence-unit/>中定义的唯一persistence.xml。{/ p>

相同的代码和配置在Spring 2.x中运行良好。当我升级到Spring 3时,在服务器中部署应用程序时会看到以下异常:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [javax.persistence.EntityManagerFactory] is defined: expected single bean but found 2

有人遇到过这个问题吗?升级到Spring 3时我错过了什么吗? 我真的很感激任何回复。

我正在使用Spring 3和Hibernate以及JPA。

问题不是含糊不清;我正在声明两个EntityManagerFactory bean并按如下方式注入它们:

  <bean id="oracleJpaTemplate" class="org.springframework.orm.jpa.JpaTemplate">
   <property name="entityManagerFactory" ref="entityManagerFactory"></property>
  </bean>

  <bean id="sqlJpaTemplate" class="org.springframework.orm.jpa.JpaTemplate">
   <property name="entityManagerFactory" ref="sqlEntityManagerFactory"></property>
  </bean>

这是完整的堆栈跟踪:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.flex.core.io.JpaHibernateConfigProcessor#0': Invocation of init method failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [javax.persistence.EntityManagerFactory] is defined: expected single bean but found 2
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1412)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:322)
    ... 59 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [javax.persistence.EntityManagerFactory] is defined: expected single bean but found 2
    at org.springframework.beans.factory.BeanFactoryUtils.beanOfTypeIncludingAncestors(BeanFactoryUtils.java:309)
    at org.springframework.flex.core.io.JpaHibernateConfigProcessor.afterPropertiesSet(JpaHibernateConfigProcessor.java:21)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1469)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1409)
    ... 66 more

4 个答案:

答案 0 :(得分:7)

像这样注入您的EntityManager:

@PersistenceContext(unitName = "unit1")
private EntityManager entityManager;

或您的EntityManagerFactory如下:

@PersistenceUnit(unitName = "unit1")
private EntityManagerFactory entityManagerFactory;

(在您的上下文中可能需要<context:annotation-config/>才能实现此目的)

答案 1 :(得分:2)

我的问题解决了。 spring-flex-core库是罪魁祸首。我使用的1.5.0.M1版本不允许多个EntityManagerFactories。当我使用1.5.0.RELEASE时,错误消失了。以下文章很有帮助 http://forum.springsource.org/showthread.php?100273-JpaHibernateConfigProcessor-complains-when-multiple-EntityManagers-present

答案 2 :(得分:1)

某处你有一个org.springframework.flex.core.io.JpaHibernateConfigProcessor类型的bean,看起来它有一个使用javax.persistence.EntityManagerFactory自动装配的init方法。由于有两个,它失败了。它可能在XML中显式声明,如:

<bean class="org.springframework.flex.core.io.JpaHibernateConfigProcessor"/>

注意没有id属性,因此long bean名称以“#0”结尾。

编辑:第二个想法,该类的名称看起来像是在ApplicationContext启动时运行的某种bean处理器,所以它可能是由自定义的spring命名空间元素或其他一些机制创建的。

答案 3 :(得分:0)

你必须先做所有的事情,比如定义 数据源 eventManagerFactory 事务管理器 persistenceUnits(必须在单独的persistence.xml文件中)等 然后编译它将正常工作 也不要忘记在baseDao类中添加注释 as @applicationContext(unitName =“example”)