这几天配置spring data jpa时,使用声明式事务碰到了一个奇怪的问题,无论怎么配置xml或在方法上使用@Transactional(value = "tm1")都会报如下找不到事务管理器的错误:
Exception in thread "main" org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [org.springframework.transaction.PlatformTransactionManager] is defined: expected single matching bean but found 2: tm2,tm1
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.$Proxy20.save(Unknown Source)
at org.fxbird.springjpa.service.DepartmentServiceImpl.addDeptAndEmp(DepartmentServiceImpl.java:47)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
at com.sun.proxy.$Proxy24.addDeptAndEmp(Unknown Source)
at org.fxbird.springjpa.App.main(App.java:15)
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.$Proxy20.save(Unknown Source)
at org.fxbird.springjpa.service.DepartmentServiceImpl.addDeptAndEmp(DepartmentServiceImpl.java:47)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
at com.sun.proxy.$Proxy24.addDeptAndEmp(Unknown Source)
at org.fxbird.springjpa.App.main(App.java:15)
我的配置:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <aop:config> <aop:pointcut id="serviceOperation" expression="execution(* org.fxbird.springjpa.service.*ServiceImpl.*(..))"/> <aop:advisor pointcut-ref="serviceOperation" advice-ref="txAdvice"/> </aop:config> <tx:advice id="txAdvice" transaction-manager="tm1"> <tx:attributes> <tx:method name="find*" read-only="true"/> <tx:method name="count*" propagation="NEVER"/> <tx:method name="*" /> </tx:attributes> </tx:advice> <bean abstract="true" id="baseDS" class="org.apache.commons.dbcp.BasicDataSource" lazy-init="true"> <property name="initialSize" value="3"/> <property name="username" value="root"/> <property name="password" value="root"/> </bean> <bean id="dataSource1" parent="baseDS"> <property name="url"> <value>jdbc:mysql://localhost/company?useUnicode=true&characterEncoding=utf-8</value> </property> </bean> <bean id="dataSource2" parent="baseDS"> <property name="url"> <value>jdbc:mysql://localhost/employee?useUnicode=true&characterEncoding=utf-8</value> </property> </bean> <bean abstract="true" id="baseEmf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="packagesToScan" value="org.fxbird.springjpa.model"/> <property name="jpaVendorAdapter"> <bean class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter"/> </property> <property name="jpaProperties"> <props> <prop key="databasePlatform">org.eclipse.persistence.platform.database.MySQLPlatform</prop> <prop key="eclipselink.jdbc.cache-statements">true</prop> <prop key="eclipselink.weaving">false</prop> <prop key="eclipselink.logging.level">FINEST</prop> <prop key="eclipselink.allow-zero-id">true</prop> <!--<prop key="eclipselink.target-server">com.atomikos.eclipselink.platform.AtomikosTransactionController</prop>--> <!--<prop key="eclipselink.external-transaction-controller">true</prop>--> </props> </property> </bean> <bean id="emfCompany" parent="baseEmf"> <property name="dataSource" ref="dataSource1"/> </bean> <bean id="emfEmp" parent="baseEmf"> <property name="dataSource" ref="dataSource2"/> </bean> <bean id="tm1" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="emfCompany"/> </bean> <bean id="tm2" name="tm2" class="org.springframework.orm.jpa.JpaTransactionManager"> <qualifier value="tm2"/> <property name="entityManagerFactory" ref="emfEmp"/> </bean> <context:component-scan base-package="org.fxbird.springjpa.service" /> <jpa:repositories base-package="org.fxbird.springjpa.repo.company" entity-manager-factory-ref="emfCompany" transaction-manager-ref="tm1"/> <jpa:repositories base-package="org.fxbird.springjpa.repo.emp" entity-manager-factory-ref="emfEmp" transaction-manager-ref="tm2"/> <tx:annotation-driven /> </beans>
后来我找来了书上的例子,却一切正常,仔细对比了代码的不同好几遍,没发现任何异常,头发却快掉了好几十根了。第二天又试了一下,忽然怀疑是不是有可能jar包版本不同导致的,书上的例子用的是spring 4.0.2,而我用的是4.1.4,换成书上的版本一运行果然错误没了,shit,困扰我好几天了,又换成最新的4.1.6,也没有问题,看来就是版本问题了。
相关推荐
spring声明式事务实例 可复制修改使用。。。。。。。。。。
Spring声明式事务配置管理方法
Spring源代码解析(六):Spring声明式事务处理.doc
spring声明式事务处理demo。myeclipse工程
Spring 声明式事务和Spring 编程式事务
spring编程式事务与声明式事务详解,超详细!
1. 基于Aspectj实现动态数据源...6. 实现事务内切换数据源(支持原生Spring声明式事务哟,仅此一家),并支持多数据源事务回滚(有了它除了跨服务的事务你需要考虑分布式事务,其他都不需要,极大的减少了系统的复杂程度)
Java高级编程 实验报告 spring 声明事务 实验目的 掌握spring 声明式事务管理配置 实验环境 本实验采用本实验采用的eclipse或者 Myeclpse开发工具。Spring 4.0以上 Jdk1.7以上、oracle/mysql。
<?xml version="1.0" encoding="UTF-8"?><beansxmlns=...
NULL 博文链接:https://babalaaaa.iteye.com/blog/538687
spring+mybatis的声明式事务
spring声明式事务管理异常处理的测试
spring声明式事务.zip
在Spring3中配置声明式事务比早期版本显得更加简便。只需要几行配置文件+注解就可以实现面向切面的AOP事务
1.掌握Myeclipse的使用。 2.掌握spring框架和hibernate框架的使用。 3. 掌握整合spring和hibernate的持久化操作编程 4.掌握基于AOP的声明式事务编程...3.配置WEB-INF/applicationContext.xml提供基于AOP的声明式事务
示例代码 博文链接:https://awaken2012.iteye.com/blog/1728283
此外,Spring事务管理器支持多种类型的事务策略,包括不同的传播行为和隔离级别,允许开发者根据具体业务场景选择最合适的事务管理策略。深入理解Spring声明式事务的工作原理,不仅能帮助开发者更高效地使用Spring...
本代码使用H2内存数据库演示spring事务使用,包括编程式事务,声明式事务@Transactional使用,自定义事务事务注解实现自定义事务管理器
Spring_Hibernate使用TransactionInterceptor声明式事务配置.doc