在spring-context.xml中配置即可(這隻貼了關鍵信息):
<!--事物配置 -->
<!--配置aop切面-->
<aop:config proxy-target-class="true">
<!--切面配置:表達式:控制到方法上:修飾符 返回值 方法(參數) ===》返回值*:表示任何返回值,參數:(..):表示任意個參數
注意:在定位參數的時候,com.wb.service.impl.*.*.*(..)
表示com.wb.service.impl.user.xxxximpl.xx方法(任意個方法參數);當然,也可以表示com.wb.service.impl.xxxximpl.xx方法(任意個方法參數),就是少了user這一層
,就是說(..) 前面的第一個*表示的是方法,注意在寫這個表述式後,創建service的實現的時候,路徑一定要按照這個規則來,不然不能被事務控制
-->
<aop:pointcut id="serviceMethods"
expression="execution(public * com.wb.service.impl.*.*.*(..))"/>
<!--通知調用-->
<aop:advisor advice-ref="transactionAdvice" pointcut-ref="serviceMethods"/>
</aop:config>
<!--配置事務管理器-->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--配置事務的傳播屬性和異常處理-->
<tx:advice transaction-manager="transactionManager" id="transactionAdvice">
<tx:attributes>
<tx:method name="read*" read-only="true"/>
<tx:method name="get*" read-only="true"/>
<tx:method name="load*" read-only="true"/>
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>