zoukankan      html  css  js  c++  java
  • 27Spring_的事务管理_银行转账业务加上事务控制_基于tx.aop进行声明式事务管理

    上一篇文章中,银行转账业务没有使用事务,会出现问题,所以这篇文章对上篇文章出现的问题进行修改。

    事务 依赖 AOP , AOP需要定义切面, 切面由Advice(通知) 和 PointCut(切点) 组成 !

    项目结构图:

    这个案例和前一篇文章的案例是一样的,我们修改的其实只是ApplicationContext.xml文件,其他代码不会变所以这里就不多做解释了。

    直接给出ApplicationContext.xml代码。里面的注释好好看一下。

    <?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:aop="http://www.springframework.org/schema/aop"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xmlns:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
                               http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                               http://www.springframework.org/schema/aop 
                               http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
                               http://www.springframework.org/schema/context 
                               http://www.springframework.org/schema/context/spring-context-2.5.xsd
                               http://www.springframework.org/schema/tx 
                               http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
     
    <!-- <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>   -->
    <!-- 引入peoperties文件 -->
    <!-- <context:property-placeholder location="classpath:db.properties"/> -->
    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations" value="classpath:db.properties"/>
    </bean>
    
     
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
      <property name="driverClass" value="${driver}"/>
      <property name="jdbcUrl" value="${url}"></property>
     <property name="user" value="${username}"></property>
    <property name="password" value="${password}"></property>
    </bean>  
     
     
     
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource" ref="dataSource"/>
    </bean>
    
    <bean id="AccountDao" class="cn.itcast.dao.AccountDaoimpl">
    <property name="jdbcTemplate" ref="jdbcTemplate"/>
    </bean>
    <bean id="AccountService" class="cn.itcast.service.AccountServiceimpl">
    
    </bean>
    <!-- 第一步:配置事务管理器 -->
    <!-- 事务管理器 
    之前提过的。事务管理器针对不同的持久层有不同的类,比如Spring自带的SpringJdbc,Hibernata等等。这里用的
    就是Sping自带的Jdbc,所以这里用的类是DataSourceTransactionManager
    -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <!-- 
    为什么要把dataSource交给transactionManager(事务管理器),
    因为事务管理器,需要从数据源获取连接,才能开启事务,提交事务,回滚事务 -->
      <property name="dataSource" ref="dataSource"></property>
    </bean>
    <!--第二步: 配置通知, -->
    <!-- 需要通知(Advice),用来对事务增强 。其实advice代表的是环绕前,环绕后要干的事情,
    环绕前,环绕后用来干嘛,不就是开启事务,提交事务吗?但是开启事务,提交事务由什么来完成呢?不就是
    transactionManager来做。
    
    -->
    <tx:advice id="transactionAdvice"  transaction-manager="transactionManager">
    
    <!-- 接下来配事务的配置属性,为什么要配?这样的话,对什么方法,应用怎样的事务配置就知道了。这些配置信息就会成为TransactionDefinition对象 
    TransactionDefinition里面有什么还记得吗?
    之前的论文提过的有四类:1.以ISOLATION开头的 ISOLATION_* :事务隔离级别
                      2.PROPAGATION_* 事务的传播行为等四个。
    
    -->
    <tx:attributes>
    <!-- 
    name 方法名
    isolation 隔离级别
    propagation 传播行为
     timeout 超时时间
     read-only 是否只读 (false代表可以更改,true的话代表不能更改)
     
     rollback-for 配置一些异常类型,发生这些异常 回滚事务。
    no-rollback-for 配置一些异常类型,发生这些异常不 回滚事务。
    如果没有配置上面两个,默认发生什么异常都要回滚,
    -->
      <tx:method name="transfer" isolation="DEFAULT" propagation="REQUIRED"  timeout="-1" read-only="false"/>
    </tx:attributes>
    </tx:advice>
    




    <!-- 通知都有了接下来第三步就是配置切面了 --> <aop:config> <aop:advisor advice-ref="transactionAdvice" pointcut="execution(* cn.itcast.service.AccountServiceimpl.*(..) )"/> </aop:config> </beans>

    执行结果:已经有事务的控制了。要不一起成功要不都不成功。

    注意!注意!

    Spring事务管理Advice,是基于Spring 传统AOP, 所以使用aop :advisor配置

  • 相关阅读:
    thinkphp3.2 无法加载模块
    php 使用 wangeditor3 图片上传
    nginx 配置 server
    oracle练手(一)
    Oracle练习(一)
    java运算符优先级
    数据库(mysql和oracle)
    java实现4种内部排序
    mysql-----分库分表
    NIO总结-----Buffer
  • 原文地址:https://www.cnblogs.com/shenxiaoquan/p/5733865.html
Copyright © 2011-2022 走看看