1.配置普通的 controller,service ,dao 的bean.

复制代码
<!-- 配置 dao ,service -->
    <bean id="bookShopDao" class="com.liujl.spring.tx.xml.BookShopDaoImpl">
        <property name="jdbcTemplate" ref="jdbcTemplate"></property>
    </bean>
    
    <bean id="bookShopService" class="com.liujl.spring.tx.xml.serivice.impl.BookShopServiceImpl">
        <property name="bookShopDao" ref="bookShopDao"></property>
    </bean>
    
    <bean id="cashier" class="com.liujl.spring.tx.xml.serivice.impl.CashierImpl">
        <property name="bookShopService" ref="bookShopService"></property>
    </bean>
复制代码

2.配置事务管理器bean

<!-- 配置事务管理器 hibernate、jpa都是类似的这样配 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

3.引入 tx 命名空间

xmlns:tx="http://www.springframework.org/schema/tx"

4.配置事务各个属性(方法名可以使用通配符*)

复制代码
<!-- 配置事务属性 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="purchase" propagation="REQUIRED"/>
            <tx:method name="checkout" propagation="REQUIRED"/>
            <tx:method name="get*" read-only="true"/>            
            <tx:method name="find*" read-only="true"/>            
        </tx:attributes>
    </tx:advice>
复制代码

5.引入aop命名空间

xmlns:aop="http://www.springframework.org/schema/aop"

6.配置事务的切点,并把事务切点和事务属性关联起来

<!-- 配置事务的切点,并把事务切点和事务属性不关联起来 -->
    <aop:config>
        <aop:pointcut expression="execution(* com.liujl.spring.tx.xml.serivice.*.*(..))" id="txPointCut"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/>
    </aop:config>
复制代码
 事务的属性:  
    1.使用 propagation 声明事务的传播属性,默认即 REQUIRED 即被包含在上面的事务中,放弃自己处理事务
    REQUIRES_NEW 以本方法为执行单位,开启一个新事务(外部事务在方法执行前后被挂起)
    
    2.使用 isolation 指定事务的隔离级别,最常用的取值为 READ_COMMITTED 读与提交
    3.默认情况下Spring 的声明式事务对所有的运行时一样长进行回滚。
    也可以对应的属性指定配置,通常情况下取默认值即可。使用 noRollbackFor 指定不回滚的异常,其他的类似
    4.使用 readOnly 指定事务是否为只读,表示这个事务只读取数据但不更新数据,
    这样可以帮助数据库引擎优化事务。若真的是一个只读取数据库值得方法,应设置readOnly=true
    
    5.使用 timeout 指定强制回滚之前事务可以占用的时间
复制代码
posted on 2015-11-30 22:20  jdonson  阅读(1253)  评论(0编辑  收藏  举报