Spring事务配置方式(一) 拦截器方式配置

一、使用<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

原文地址:https://www.cnblogs.com/binbang/p/5063295.html