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配置方法比这篇文章讲的注解配置用的更多。
说明:本系列文章后面还有三大框架的整合内容,这一块暂时不看了。
28Spring_的事务管理_银行转账业务加上事务控制_基于注解进行声明式事务管理的更多相关文章
- spring基于注解的声明式事务控制
package com.hope.service.impl;import com.hope.dao.IAccountDao;import com.hope.domain.Account;import ...
- 阶段3 2.Spring_10.Spring中事务控制_7 spring基于注解的声明式事务控制
创建新项目 复制上一个pom.xml的内容.依赖和打包的方式 再复制src的代码过来 bean.xml.多导入context的声明 Service的实现类增加注解 dao的set方法删掉 通过Auto ...
- spring基于XML的声明式事务控制
<?xml version="1.0" encoding="utf-8" ?><beans xmlns="http://www.sp ...
- spring基于xml的声明式事务控制配置步骤
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- 阶段3 2.Spring_10.Spring中事务控制_6 spring基于XML的声明式事务控制-配置步骤
环境搭建 新建工程 把对应的依赖复制过来 src下内容复制 配置spring中的声明事物 找到bean.xml开始配置 配置事物管理器 里面需要注入DataSource 2-配置事物通知 需要先导入事 ...
- 27Spring_的事务管理_银行转账业务加上事务控制_基于tx.aop进行声明式事务管理
上一篇文章中,银行转账业务没有使用事务,会出现问题,所以这篇文章对上篇文章出现的问题进行修改. 事务 依赖 AOP , AOP需要定义切面, 切面由Advice(通知) 和 PointCut(切点) ...
- spring注解开发-声明式事务(源码)
1. 环境搭建与测试 1)导入相关依赖 数据源.数据库驱动.Spring-jdbc模块 <dependency> <groupId>org.springframework< ...
- 阶段3 2.Spring_10.Spring中事务控制_8 spring基于纯注解的声明式事务控制
新建项目 把之前项目src下的内容全部复制过来 pom.xml内复制过来 开始配置 新建一个config的包,然后再新建配置文件类SpringConfiguration @Configuration这 ...
- 基于XML的声明式事务控制
1.maven依赖 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="h ...
随机推荐
- Sharepoint学习笔记—习题系列--70-573习题解析 -(Q115-Q117)
Question 115You create a timer job.You need to debug the timer job.To which process should you attac ...
- IOS Quartz2D简介
Quartz2D 简介( 后续会有相关应用) 第一部分 绘制直线 代码示例: - (void)drawRect:(CGRect)rect{ //获取图形上下文 CGContextRef cxConte ...
- Swift Standard Library: Documented and undocumented built-in functions in the Swift standard library – the complete list with all 74 functions
Swift has 74 built-in functions but only seven of them are documented in the Swift book (“The Swift ...
- javascript的模块化解读
AMD是RequireJS在推广过程中对模块定义的规范化产出. 异步加载模块,依赖前置,提前执行. Define定义模块 define(['require','foo'],function(){ret ...
- 推送(PUSH)
一.本地推送 1.建立本地推送 UILocalNotification *notification = [[UILocalNotification alloc] init]; //推送的内容 noti ...
- Dev Grid拖拽移动行
效果图 源码下载 拖拽时带行截图效果实现代码 /// <summary> /// 拖拽帮助类 /// </summary> public static class DragHe ...
- 分享git的常用命令
Git操作笔记 1.创建目录 $ mkdir learngit $ cd learngit 2.把新建的目录变成仓库 $ git ...
- python操作db2和mysql ,ibm_db
我需要提取mysql和db2的数据进行对比,所以需要用python对其都进行操作. python对mysql进行操作应该没什么问题,就是安装drive后就可以了,在上一篇中有讲安装python-mys ...
- ppt
放映时 F5是从头开始放映, shift+F5是从当前页开始放映 在菜单->幻灯片放映->勾选 “使用演讲者视图” 就可以在播放时看到自己的备注
- 【mysql】关于checkpoint机制
一.简介 思考一下这个场景:如果重做日志可以无限地增大,同时缓冲池也足够大,那么是不需要将缓冲池中页的新版本刷新回磁盘.因为当发生宕机时,完全可以通过重做日志来恢复整个数据库系统中的数据到宕机发生的时 ...