1.使用AspectJ的xml配置方式配置事務管理:
<!-- 配置事務管理,對事務管理的操作都是基於此類而生-->
<bean id = "transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" >
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 配置事務的通知:增強事務 -->
<tx:Advice id = "txAdvice" transaction-namager="transactionManager">
<tx:attributes>
<tx:method name = "需要事務管理的方法的名字" propagation="傳播行爲" ----隔離級別、只讀等都可以設置在後面> </tx:method>
</tx:attributes>
</tx:Advice>
<!-- 配置切面 -->
<aop:config>
<aop:pointcut exepression="execution(* com.cn.AccountServiceImpl.*(..))" pointcut="pointcut1"></aop:pointcut>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut1"></aop:advisor>
</aop:config>
2.基於註解的方式
<!-- 配置事務管理,對事務管理的操作都是基於此類而生-->
<bean id = "transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" >
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 配置事務管理,對事務管理的操作都是基於此類而生-->
<bean id = "transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" >
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 開啓事務註解掃描-->
<tx:annotation-driven transaction-manager="transactionManager"/></tx:annotation-driven>
<!-- 在需要進行事務管理的類上進行註解-->
@Transactional(propagation=Propagation.REQUIRED,isolation =Isolation.DEFAULT)