2015-05-21 115 views
0

我有一個使用Spring的maven多模塊項目。我有一個核心項目它擁有所有的數據訪問bean以及entityManagerFactoryBean。Spring/maven多模塊項目:NoUniqueBeanDefinitionException

現在我已經從核心項目的背景下的XML文件導入上下文XML的另一個行家模塊項目。我試圖通過API在項目指定此覆蓋的entityManagerFactory豆:

<beans:bean parent="entityManagerFactory"> 
    <beans:property name="packagesToScan"> 
     <beans:array> 
      <beans:value>com.blah.data.domains</beans:value> 
      <beans:value>com.blah.api.domains</beans:value> 
     </beans:array> 
    </beans:property> 
</beans:bean> 

這個bean的配置是核心項目內:

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 
    <property name="dataSource" ref="dataSource"/> 
    <property name="persistenceUnitName" value="entityManager"/> 
    <property name="packagesToScan" value="com.blah.data.domains" /> 
    <property name="jpaVendorAdapter"> 
     <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> 
      <property name="showSql" value="true" /> 
      <property name="generateDdl" value="true" /> 
      <property name="databasePlatform" value="org.hibernate.dialect.PostgreSQLDialect" /> 
     </bean> 
    </property> 
    <!-- put any ORM specific stuff here --> 
    <property name="jpaProperties"> 
     <props> 
      <prop key="hibernate.hbm2ddl.auto">none</prop> 
     </props> 
    </property> 
</bean> 

正如你可以在API項目中看到,我將com.blah.api.domains添加到packagesToScan屬性中。問題是,我得到這個問題:

Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [javax.persistence.EntityManagerFactory] is defined: expected single matching bean but found 2: entityManagerFactory,entityManagerFactory$child#0 
+1

API項目配置已經在掃描com.blah.data.domains。 您核心項目再次做同樣的 錯誤是告訴entityManagerFactory的子屬性也定義爲entityManagerFactory。我沒有在上面的條目中看到這一點。但是,你可以嘗試刪除重複的條目,看看是否修復它? – Mukus

回答

3

不要試圖重寫的entityManagerFactory您的API模塊中只放一個通配符在你的核心模塊掃描包。

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 
    <property name="dataSource" ref="dataSource"/> 
    <property name="persistenceUnitName" value="entityManager"/> 
    <property name="packagesToScan" value="com.blah.*.domains" /> 
    <property name="jpaVendorAdapter"> 
     <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> 
      <property name="showSql" value="true" /> 
      <property name="generateDdl" value="true" /> 
      <property name="databasePlatform" value="org.hibernate.dialect.PostgreSQLDialect" /> 
     </bean> 
    </property> 
    <!-- put any ORM specific stuff here --> 
    <property name="jpaProperties"> 
     <props> 
      <prop key="hibernate.hbm2ddl.auto">none</prop> 
     </props> 
    </property> 
</bean> 

隨着com.blah.*.domains所有域將被掃描和加載。

+0

謝謝你的作品。 – Dean