zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    css 之 input 的提交样式
    JSON 没错又是它!!!
    json 对象解析 function 里面的return 和return false 查找字符串最后的结尾字符
    sql 中update 对字符串进行批量替换
    17 常用模块
    16 模块深入
    15 模块
    14 生成器,生成器表达式,内置函数,面向过程编程
    13 迭代器
    12 递归 三元表达式 列表生成式 字典生成式 匿名函数
  • 原文地址:https://www.cnblogs.com/binbang/p/5063295.html
Copyright © 2011-2022 走看看