一、使用<tx:advice>和<aop:config>配置事務
<!-- 配置事務管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- 注解方式配置事物 --> <!-- <tx:annotation-driven transaction-manager="transactionManager" /> --> <!-- 攔截器方式配置事物 --> <tx:advice id="transactionAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="add*" propagation="REQUIRED" /> <tx:method name="insert*" propagation="REQUIRED" /> <tx:method name="update*" propagation="REQUIRED" /> <tx:method name="modify*" propagation="REQUIRED" /> <tx:method name="delete*" propagation="REQUIRED" /> <tx:method name="delAndRepair" propagation="REQUIRED" /> <tx:method name="get*" propagation="SUPPORTS" /> <tx:method name="find*" propagation="SUPPORTS" /> <tx:method name="search*" propagation="SUPPORTS" /> <tx:method name="*" propagation="SUPPORTS" /> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut id="transactionPointcut" expression="execution(* com.fog.travel.service..*impl.*(..))" /> <aop:advisor pointcut-ref="transactionPointcut" advice-ref="transactionAdvice" /> </aop:config>
而對於<aop:config>具體解釋為:
表示com.fog.travel.service包下的所有方法為為事務管理。 expression="execution(* com.fog.travel.service.impl.*.*(..))" 這樣寫應該就可以了 這是com.fog.travel.service..*impl包下所有的類的所有方法。。 第一個*代表所有的返回值類型 第二個*代表所有的類 第三個*代表類所有方法 最后一個..代表所有的參數。
注意事項:<beans>中要加入“xmlns:aop”的命名申明,並在“xsi:schemaLocation”中指定aop配置的schema的地址 ,才會正確的顯示tx標簽
<?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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
具體標簽信息:
屬性 | 是否必須 | 默認值 | 描述 |
name | YES | 與事務屬性關聯的方法名。通配符(*)可以用來指定一批關聯到相同的事務屬性的方法。 如:'get*'、'handle*'、'on*Event'等等。 | |
propagation | NO | REQUIRED | 事務傳播行為 |
isolation | NO | DEFAULT | 事務隔離級別 |
timeout | NO | -1 | 事務超時的時間(以秒為單位) |
read-only | NO | false | 事務是否只讀? |
rollback-for | NO | 將被觸發進行回滾的 Exception(s);以逗號分開。 如:'com.foo.MyBusinessException,ServletException' | |
no-rollback-for | NO | 不 被觸發進行回滾的 Exception(s);以逗號分開。 如:'com.foo.MyBusinessException |
其他詳解:http://tonl.iteye.com/blog/1966075