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

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

28Spring_的事务管理_银行转账业务加上事务控制_基于注解进行声明式事务管理的更多相关文章

  1. spring基于注解的声明式事务控制

    package com.hope.service.impl;import com.hope.dao.IAccountDao;import com.hope.domain.Account;import ...

  2. 阶段3 2.Spring_10.Spring中事务控制_7 spring基于注解的声明式事务控制

    创建新项目 复制上一个pom.xml的内容.依赖和打包的方式 再复制src的代码过来 bean.xml.多导入context的声明 Service的实现类增加注解 dao的set方法删掉 通过Auto ...

  3. spring基于XML的声明式事务控制

    <?xml version="1.0" encoding="utf-8" ?><beans xmlns="http://www.sp ...

  4. spring基于xml的声明式事务控制配置步骤

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  5. 阶段3 2.Spring_10.Spring中事务控制_6 spring基于XML的声明式事务控制-配置步骤

    环境搭建 新建工程 把对应的依赖复制过来 src下内容复制 配置spring中的声明事物 找到bean.xml开始配置 配置事物管理器 里面需要注入DataSource 2-配置事物通知 需要先导入事 ...

  6. 27Spring_的事务管理_银行转账业务加上事务控制_基于tx.aop进行声明式事务管理

    上一篇文章中,银行转账业务没有使用事务,会出现问题,所以这篇文章对上篇文章出现的问题进行修改. 事务 依赖 AOP , AOP需要定义切面, 切面由Advice(通知) 和 PointCut(切点) ...

  7. spring注解开发-声明式事务(源码)

    1. 环境搭建与测试 1)导入相关依赖 数据源.数据库驱动.Spring-jdbc模块 <dependency> <groupId>org.springframework< ...

  8. 阶段3 2.Spring_10.Spring中事务控制_8 spring基于纯注解的声明式事务控制

    新建项目 把之前项目src下的内容全部复制过来 pom.xml内复制过来 开始配置 新建一个config的包,然后再新建配置文件类SpringConfiguration @Configuration这 ...

  9. 基于XML的声明式事务控制

    1.maven依赖 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="h ...

随机推荐

  1. Android--Apache HttpClient

    前言 上一篇文章介绍了使用HttpURLConnection来完成对于HTTP协议的支持.现在介绍一个新的方式来访问Web站点,那就是HttpClient. HttpClient是Apache开源组织 ...

  2. android 判断屏幕是否亮着

    PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE); boolean screen = p ...

  3. 我遇到的CocoaPods的问题(也许后期会解决,持续更新)

    在此博客中写下两类关于CocoaPods的问题: 未解决的问题:可以留着以后解决 已经解决的问题:可以备份以后回头再参考解决同样的问题 <已解决的问题> 解决方法是:pod install ...

  4. 多线程基础(五)NSThread线程通信

    5.多线程基础 线程间通信   什么叫线程间通信 在一个进程中,线程往往不是孤立存在的,多个线程之间需要经常进行通信   线程间通信的体现 1个线程传递数据给另一个线程 在1个线程中执行完特定任务后, ...

  5. IOS-Swift、Objective-C、C++混合编程

    1.Objective-C调用C++代码 后缀为m文件的是Objective-C的执行文件,而后缀为mm文件的是Objective-C++文件. 直接在Objective-C中是无法调用C++代码的, ...

  6. 通过设置虚拟机(ubantu15.10)的分辨率达到全屏效果

    最近搭建了一个ubantu 15.10虚拟机,怎么折腾都不能全屏显示.虽然我已经点了,  还是无法达到真正的全屏. 查了一下,http://jingyan.baidu.com/article/0964 ...

  7. SAM4E单片机之旅——17、通过UART进行标准IO

    交互还是很有必要的,而且使用键盘和显示器的交互效率还是很高的.当然,可以直接使用UART进行字符的输入和输出.但是又何必浪费了C的标准输入输出的格式控制之类的功能呢? 这次内容就是使用scanf() ...

  8. Eclipse EE 发布项目导致 Tomcate 的配置文件 server.xml 还原

    在server.xml中配置SSL时,发现了每次发布项目都导致server.xml被还原了: <Connector port="8443" protocol="or ...

  9. 最小安装centos 7 无GUI静默安装 oracle 12c,打造轻量linux化服务器

    CentOS 7 下载地址:http://mirrors.opencas.cn/centos/7/isos/x86_64/CentOS-7-x86_64-Everything-1511.iso 一.安 ...

  10. Android 开发之Windows环境下Android Studio安装和使用教程(图文详细步骤)

    鉴于谷歌最新推出的Android Studio备受开发者的推崇,所以也跟着体验一下. 一.介绍Android Studio  Android Studio 是一个Android开发环境,基于Intel ...