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配置方法比这篇文章讲的注解配置用的更多。

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

原文地址:https://www.cnblogs.com/shenxiaoquan/p/5734041.html