下面使用spring.xml进行配置事务:(注解和xml同时注入时优先使用注解)
可以发现在配置需要事务的方法时使用通配符 *,这样会使指定格式的方法注入事务,而不是指定格式的方法则不会启动事务。
<!--声明事务切入的位置-->
<aop:config>
<aop:pointcut id="transactionCut" expression="execution(* cn.cdulm.service.impl.*.*(..))"/>
</aop:config>
<!--预设什么样的方法需要使用事务-->
<tx:advice>
<tx:attributes>
<!--使用通配符 * 表示后续字符-->
<tx:method name="add*"/> <!--所有开头是add的方法,如:add()、addUser()、addPerson()等-->
<tx:method name="update*"/> <!--所有开头是update的方法,如:update()、updateUser()、updatePerson()等-->
<tx:method name="delete*"/> <!--同理-->
<tx:method name="get*"/> <!--同理-->
<tx:method name="select*"/> <!--同理-->
</tx:attributes>
</tx:advice>