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

    将applicationContext.xml 和 AccountServiceImpl 给备份一个取名为applicationContext2.xml 和 AccountServiceImpl2.java

    第一步:配置事务管理器

    第二步:配置注解驱动

    以上两步是在ApplicationContext2.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.AccountServiceimpl2">
    
    </bean>


    <!--使用注解的两个配置 --> <!-- 第一步配置事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 第二步:配置注解驱动,注解进行事务管理 --> <tx:annotation-driven transaction-manager="transactionManager"/> </beans>

    第三步:在AccountServiceImpl2.java中完成, 在需要管理事务的业务类(业务方法)上添加@Transactional 注解

    import org.springframework.beans.factory.annotation.Autowired;
    
    
    import org.springframework.transaction.annotation.Transactional;
    
    import cn.itcast.dao.AccountDao;
    /*
    
     *转账接口的实现类
     */
    public class AccountServiceimpl2 implements AccountService {
    
      /**
       * 这个类是转账接口的实现类。我们要考虑的问题是怎么把Dao给注入进去。
       * 这里用注解的方式 @Autowired。
       * 一旦用这种方式,之前那种set/get方式在applicationContext.xml中的
       * <bean id="AccountService" class="cn.itcast.service.AccountServiceimpl">
         <!--  <property name="accountDao" ref="AccountDao"/>  -->
          </bean>
          里面的property name="accountDao" ref="AccountDao"/>必须去掉。
       * 
       * 
       */
      @Autowired
        private AccountDao accountDao;
    
    @Transactional
        public void transfer(String outAccount, String inAccount, double money) {
            
        accountDao.addmoney(inAccount, money);
    //这里加上异常之后,就执行不下去了到这里,就会造成说 accountDao.addmoney(inAccount, money)执行了;但是 accountDao.reducemoney(outAccount, money)没有执行。
    int a=1/0; accountDao.reducemoney(outAccount, money); } }

    以上三步就完成了注解的配置。

    看一下结果:对的。

    实际工作中:其实上一篇论文的xml配置方法比这篇文章讲的注解配置用的更多。

    说明:本系列文章后面还有三大框架的整合内容,这一块暂时不看了。

  • 相关阅读:
    财务自由之路-我用Airtest刷抖音致富
    Python基础-背单词游戏
    别人是在谈薪酬,而你是在自残
    不写一行代码,使用Airtest完成自动化测试
    精致的JavaScript代码
    浅谈排序二叉树
    JavaScript 生产者消费者模型
    IDEA 激活码全家桶 webStorm亲测可用【更新日期2020.3.30】
    Cocos-JS HTTP网络请求
    初始OpenGL
  • 原文地址:https://www.cnblogs.com/shenxiaoquan/p/5734041.html
Copyright © 2011-2022 走看看