[转]spring tx:advice 和 aop:config 配置事务

 
  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <beans xmlns="http://www.springframework.org/schema/beans" 
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  4. xmlns:aop="http://www.springframework.org/schema/aop" 
  5. xmlns:tx="http://www.springframework.org/schema/tx" 
  6. xmlns:context="http://www.springframework.org/schema/context" 
  7. xsi:schemaLocation="http://www.springframework.org/schema/beans 
  8.                      http://www.springframework.org/schema/beans/spring-beans.xsd 
  9.                      http://www.springframework.org/schema/tx 
  10.                      http://www.springframework.org/schema/tx/spring-tx.xsd 
  11.                      http://www.springframework.org/schema/aop 
  12. <bean id="transactionManager" 
  13. class="org.springframework.orm.hibernate3.HibernateTransactionManager" 
  14. abstract="false" lazy-init="default" autowire="default" 
  15. dependency-check="default"
  16. <property name="sessionFactory"
  17. <ref bean="sessionFactory" /> 
  18. </property
  19. </bean
  20. <tx:advice id="txAdvice" transaction-manager="transactionManager"
  21. <tx:attributes
  22. <tx:method name="add*" propagation="REQUIRED" /> 
  23. <tx:method name="delete*" propagation="REQUIRED" /> 
  24. <tx:method name="update*" propagation="REQUIRED" /> 
  25. <tx:method name="add*" propagation="REQUIRED" /> 
  26. <!-- <tx:method name="*" propagation="true" />--> 
  27. </tx:attributes
  28.     </tx:advice
  29.     <aop:config
  30. <aop:pointcut id="allManagerMethod" 
  31. expression="execution(* com.service.*.*(..))" /> 
  32. <aop:advisor advice-ref="txAdvice" 
  33. pointcut-ref="allManagerMethod" /> 
  34. </aop:config
  35. </beans

Xml代码  收藏代码
  1. <beans
  2. <tx:advice id="txAdvice" transaction-manager="transactionManager">  
  3. <tx:attributes>  
  4. <tx:method name="get*" read-only="true"/>  
  5. <tx:method name="*" propagation="REQUIRES_NEW" rollback-for="Exception"/>  
  6. </tx:attributes>  
  7. </tx:advice
  8. <!-- aop代理设置-->  
  9. <aop:config proxy-target-class="true">    
  10.  
  11. </aop:config
  12. </beans

Xml代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <beans xmlns="http://www.springframework.org/schema/beans " 
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance " 
  4. xmlns:aop="http://www.springframework.org/schema/aop " 
  5. xmlns:tx="http://www.springframework.org/schema/tx " 
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans 
  7.                      http://www.springframework.org/schema/beans/spring-beans.xsd 
  8.                      http://www.springframework.org/schema/tx 
  9.                      http://www.springframework.org/schema/tx/spring-tx.xsd 
  10.                      http://www.springframework.org/schema/aop 

属性 是否需要? 默认值 描述
name   与事务属性关联的方法名。通配符(*)可以用来指定一批关联到相同的事务属性的方法。 如:'get*'、'handle*'、'on*Event'等等。
propagation REQUIRED 事务传播行为
isolation DEFAULT 事务隔离级别
timeout -1 事务超时的时间(以秒为单位)
read-only false 事务是否只读?
rollback-for   将被触发进行回滚的 Exception(s);以逗号分开。 如:'com.foo.MyBusinessException,ServletException'
no-rollback-for   不 被触发进行回滚的 Exception(s);以逗号分开。 如:'com.foo.MyBusinessException
posted @ 2015-09-08 16:05  yunlvrensheng  阅读(555)  评论(0)    收藏  举报