没有定义类型为[org.springframework.transaction.PlatformTransactionManager]的限定bean

时间:2015-10-14 15:15:03

标签: java spring jpa eclipselink spring-data-jpa

我正在使用2个Weblogic数据源;在我的XML配置中,我有2个持久性单元,2个entityManagerFactories和2个transactionManagers。文件如下:

的persistence.xml

<persistence-unit name="modelPersistence" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider> 
</persistence-unit>

<persistence-unit name="orderPersistence" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
</persistence-unit>

config.xml中

<!-- transactionManagers -->
    <tx:annotation-driven transaction-manager="transactionManager" /> 
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean> 

    <bean id="transactionManagerLegacy" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactoryLegacy" />
    </bean>

<!-- entityManagerFactory --> 
    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSourceModel"/>
        <property name="persistenceXmlLocation" value="classpath*:META-INF/persistence.xml" />
        <property name="persistenceUnitName" value="modelPersistence" />
        <property name="jpaProperties">
            <props>
               <prop key="eclipselink.logging.level">FINE</prop>
            </props>
        </property>
        <property name="loadTimeWeaver">
            <bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>
        </property> 
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter">
                <property name="databasePlatform" value="org.eclipse.persistence.platform.database.OraclePlatform" />
                <property name="generateDdl" value="true"/>
                <property name="showSql" value="true"/>
            </bean>
        </property>
    </bean>     

<!-- entityManagerFactoryLegacy --> 
        <bean id="entityManagerFactoryLegacy" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSourceOrder"/>
        <property name="persistenceXmlLocation" value="classpath*:META-INF/persistence.xml" />
        <property name="persistenceUnitName" value="orderPersistence" />
        <property name="jpaProperties">
            <props>
               <prop key="eclipselink.logging.level">FINE</prop>
            </props>
        </property>
        <property name="loadTimeWeaver">
            <bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>
        </property>
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter">
                <property name="databasePlatform" value="org.eclipse.persistence.platform.database.OraclePlatform" />
                <property name="generateDdl" value="true"/>
                <property name="showSql" value="true"/>
            </bean>
        </property>
    </bean>

<!-- data source 1-->
     <bean id="dataSourceModel" class="org.springframework.jndi.JndiObjectFactoryBean" depends-on="model-datasource-jndi">
        <property name="jndiName" ref="model-datasource-jndi" />
        <property name="resourceRef" value="true" /> 
    </bean> 

<!-- data source 2-->
     <bean id="dataSourceOrder" class="org.springframework.jndi.JndiObjectFactoryBean" depends-on="order-datasource-jndi">
        <property name="jndiName" ref="order-datasource-jndi" />
        <property name="resourceRef" value="true" /> 
    </bean> 

<!-- configure persistenceUnitManager -->
    <bean id="persistenceUnitManager" class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
      <property name="persistenceXmlLocation" value="classpath*:META-INF/persistence.xml" />
      <property name="dataSources">
        <map>
            <entry key="jdbc/MODEL-DS" value-ref="dataSourceModel"/>
            <entry key="jdbc/ORDER-DS" value-ref="dataSourceOrder"/> 
        </map>
      </property>
    </bean> 
    <jpa:repositories base-package="org.codingpedia.demo.repositories.ModelRepository" entity-manager-factory-ref="entityManagerFactory" transaction-manager-ref="transactionManager"/>
    <jpa:repositories base-package="org.codingpedia.demo.repositories.OrderRepository" entity-manager-factory-ref="entityManagerFactoryLegacy" transaction-manager-ref="transactionManagerLegacy"/>

ModelRepository

@Repository ("ModelRepo")
@PersistenceContext(unitName = "modelPersistence")
public interface ModelRepository extends JpaRepository<Model, String> {}

orderRepository

@Repository ("OrderRepo")
@PersistenceContext(unitName = "orderPersistence")
public interface OrderRepository extends JpaRepository<Model, String> {}

有关处理多个交易管理器的正确注释的任何建议吗?我错过了别的什么吗?其他堆栈溢出示例使用DAO,我没有找到帮助,因为我使用的是存储库,模型和控制器java文件。我也没有使用hibernate,很多例子都参考了hibernate。非常感谢您的见解。

我收到以下错误:

org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [org.springframework.transaction.PlatformTransactionManager] is defined: expected single matching bean but found 2: transactionManagerLegacy,transactionManager
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:365)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:331)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.determineTransactionManager(TransactionAspectSupport.java:370)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:271)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodIntercceptor.invoke(CrudMethodMetadataPostProcessor.java:122)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
    at com.sun.proxy.$Proxy120.findAll(Unknown Source)
    at org.codingpedia.demo.controller.BaseController.index(BaseController.java:45)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:777)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:706)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:943)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:844)
    at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:280)
    at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:254)
    at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:136)
    at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:346)
    at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:243)
    at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.wrapRun(WebAppServletContext.java:3432)
    at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3402)
    at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
    at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:120)
    at weblogic.servlet.provider.WlsSubjectHandle.run(WlsSubjectHandle.java:57)
    at weblogic.servlet.internal.WebAppServletContext.doSecuredExecute(WebAppServletContext.java:2285)
    at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2201)
    at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2179)
    at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1572)
    at weblogic.servlet.provider.ContainerSupportProviderImpl$WlsRequestExecutor.run(ContainerSupportProviderImpl.java:255)
    at weblogic.work.ExecuteThread.execute(ExecuteThread.java:311)
    at weblogic.work.ExecuteThread.run(ExecuteThread.java:263)

3 个答案:

答案 0 :(得分:6)

您可以使用:

@Transactional("transactionManagerName")

请参阅org.springframework.transaction.annotation.Transactional(@Transactional)注释javadoc:

/**
 * A qualifier value for the specified transaction.
 * <p>May be used to determine the target transaction manager,
 * matching the qualifier value (or the bean name) of a specific
 * {@link org.springframework.transaction.PlatformTransactionManager}
 * bean definition.
 */
String value() default "";

答案 1 :(得分:0)

在你的情况下应该是:

<tx:annotation-driven transaction-manager="transactionManager" />
<tx:annotation-driven transaction-manager="transactionManagerLegacy" />

导致你的bean的ID。

如果您想要多个数据源我建议您阅读:http://www.baeldung.com/spring-data-jpa-multiple-databases

答案 2 :(得分:0)

目前,Dali仅支持每个项目的一个持久性单元一个持久性XML文件。其他配置可以存在于JPA项目中,但是当使用多个持久性单元时,验证和默认处理可能不正确。查看更多信息here