设为首页 加入收藏

TOP

JAVAEE——spring03:spring整合JDBC和aop事务(三)
2017-10-13 10:25:22 】 浏览:973
Tags:JAVAEE spring03 spring 整合 JDBC aop 事务
<!-- 事务模板对象 -->
<bean name="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate" >
    <property name="transactionManager" ref="transactionManager" ></property>
</bean>

   3.1.3 将事务模板注入Service

<!-- 3.Service-->
<bean name="accountService" class="cn.itcast.service.AccountServiceImpl" >
    <property name="ad" ref="accountDao" ></property>
    <property name="tt" ref="transactionTemplate" ></property>
</bean> 

   3.1.4 在Service中调用模板

    public void transfer(final Integer from,final Integer to,final Double money) {
        
        tt.execute(new TransactionCallbackWithoutResult() {
            @Override
            protected void doInTransactionWithoutResult(TransactionStatus arg0) {
                //减钱
                ad.decreaseMoney(from, money);
                int i = 1/0;//触发错误,验证代码是否正确
                //加钱
                ad.increaseMoney(to, money);
            }
        });
    }

   3.2 xml配置(aop)

   3.2.1 导包

   aop、aspect、aop联盟和weaving织入包

   3.2.2 导入新的约束(tx)

  

   beans: 最基本
   context:读取properties配置
   aop:配置aop
   tx:配置事务通知

   3.2.3 配置通知

<!-- 配置事务通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager" >
    <tx:attributes>
        <!-- 以方法为单位,指定方法应用什么事务属性
            isolation:隔离级别
            propagation:传播行为
            read-only:是否只读
         -->
        <tx:method name="save*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
        <tx:method name="persist*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
        <tx:method name="update*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
        <tx:method name="modify*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
        <tx:method name="delete*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
        <tx:method name="remove*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
        <tx:method name="get*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true" />
        <tx:method name="find*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true" />
        <tx:method name="transfer" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
    </tx:attributes>
</tx:advice>

   3.2.4 配置将通知织入目标

<!-- 配置织入 -->
<aop:config  >
    <!-- 配置切点表达式 -->
    <aop:pointcut expression="execution(* cn.itcast.service.*ServiceImpl.*(..))" id="txPc"/>
    <!-- 配置切面 : 通知+切点
             advice-ref:通知的名称
             pointcut-ref:切点的名称
     -->
    <aop:advisor advice-ref="txAdvice" pointcut-ref="txPc" />
</aop:config>

   3.3 注解配置(aop)

   导包、tx约束和xml配置(aop)相同

   3.3.1 开启注解管理事务

<!-- 开启使用注解管理aop事务 -->
<tx:annotation-driven/>

   3.3.2 使

首页 上一页 1 2 3 4 下一页 尾页 3/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇(转)Java对象克隆(Clone)及Clon.. 下一篇数据库(分库分表)中间件对比

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目