Spring中使用事务搭建转账环境方法二 相对简便的注解方法 ——配置文件注入对象属性需要setter方法 注解方法,不需要生成setter方法
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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- c3p0连接池得到dataSource -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/sw_database"></property>
<property name="user" value="root"></property>
<property name="password" value="root"></property>
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 开启事务注解 -->
<tx:annotation-driven transaction-manager="transactionManager"/> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean> <bean id="accountDao" class="com.swift.AccountDao">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean> <bean id="accountService" class="com.swift.AccountService">
<property name="accountDao" ref="accountDao"></property>
</bean> </beans>
事务的注解方法依然需要事务管理器DataSourceTransactionManager,这个管理器当然需要数据源DataSource来确认指向哪个数据库,即注入dataSource。
然后开启事务注解的扫描<tx:annotation-driven transaction-manager="transactionManager"/>
程序中在需要增强的类上用@Transactional标记就可以了,自动调用对该类所有方法进行增强的事务,不用再像前边的配置文件方法,自己定义
省去了下面步骤
<!-- 配置事务增强 -->
<tx:advice id="txadvice" transaction-manager="transactionManager">
<!-- 做事务操作 -->
<tx:attributes>
<!-- 事务操作的方法匹配规则 -->
<tx:method name="money*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
也省去了切入点、切面的aop操作
<!-- 配置切面 -->
<aop:config>
<!-- 切入点 -->
<aop:pointcut expression="execution(* com.swift.AccountService.*(..))" id="pointcut1"/>
<!-- 切面 -->
<aop:advisor advice-ref="txadvice" pointcut-ref="pointcut1"/>
</aop:config>
其他的类dao类和Service类没有改变
代码如下:
package com.swift; import org.springframework.jdbc.core.JdbcTemplate; public class AccountDao {
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
} //少钱的方法
public void lessMoney(String from,double number) {
String sql="update account set money=money-? where username=?";
jdbcTemplate.update(sql, number,from); }
//多钱的方法
public void moreMoney(String to,double number) {
String sql="update account set money=money+? where username=?";
jdbcTemplate.update(sql, number,to);
}
}
Service类
package com.swift; import org.springframework.transaction.annotation.Transactional; @Transactional
public class AccountService {
private AccountDao accountDao;
public void setAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;
} public void moneyTransfer(String from,String to,double number) {
accountDao.lessMoney(from,number);
int i=10/0;
accountDao.moreMoney(to,number);
}
}
配置文件注入对象属性需要setter方法 注解方法,不需要生成setter方法
下边是注解方法注入属性
package com.swift; import javax.annotation.Resource; public class Service {
@Resource(name="studentDao")
private StudentDao studentDao;
@Resource(name="courseDao")
private CourseDao courseDao;
public String fun() {
return "This is Service's fun()........."+this.studentDao.fun()+this.courseDao.fun();
}
}
aop操作复习
<bean id="book" class="com.swift.aop.Book"></bean>
<bean id="adviceBook" class="com.swift.aop.AdviceBook"></bean> <aop:config>
<!-- 切入点表达式第一个*表示public private等权限后接空格,第二个*表示任意方法(..)表示参数 -->
<aop:pointcut expression="execution(* com.swift.aop.Book.*())" id="pointcut1"/> <!-- 哪个切面(用来增强切入点的类) 名称是什么用ref表示 -->
<aop:aspect ref="adviceBook">
<!-- 增强的具体方法,增强哪个切入点 -->
<aop:before method="before" pointcut-ref="pointcut1"/>
</aop:aspect>
</aop:config>
只要调用book对象中的任意方法就可以得到增强后的效果
Spring中使用事务搭建转账环境方法二 相对简便的注解方法 ——配置文件注入对象属性需要setter方法 注解方法,不需要生成setter方法的更多相关文章
- Spring中使用事务搭建转账环境 转账操作,
演示不使用事务出现异常情况 Dao层两个方法lessMoney()和moreMoney() package com.swift; import org.springframework.jdbc.cor ...
- Spring中的事务操作
事务的特性 原子性:强调事务的不可分割. 一致性:事务的执行的前后数据的完整性保持一致. 隔离性:一个事务执行的过程中,不应该受到其他事务的干扰. 持久性:事务一旦结束,数据就持久化到数据库. 如果不 ...
- (转)Spring中的事务操作
http://blog.csdn.net/yerenyuan_pku/article/details/70024364 事务的回顾 什么是事务 事务是逻辑上的一组操作,组成这组操作的各个逻辑单元,要么 ...
- Spring 中的事务操作、注解、以及 XML 配置
事务 事务全称叫数据库事务,是数据库并发控制时的基本单位,它是一个操作集合,这些操作要么不执行,要么都执行,不可分割.例如我们的转账这个业务,就需要进行数据库事务的处理. 转账中至少会涉及到两条 SQ ...
- Spring中@Transactional事务回滚
转载: Spring中@Transactional事务回滚 一.使用场景举例 在了解@Transactional怎么用之前我们必须要先知道@Transactional有什么用.下面举个栗子:比如一个部 ...
- SSM-Spring-23:概念《Spring中的事务是什么?》
------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 本篇博客会详细讲述Spring中的事务,会展开来用语言解释,用于了解概念和准备面试 事务的概念: 一个或者一组 ...
- Spring中的事务管理
事务简介: 事务管理是企业级应用程序开发中必不可少的技术,用来确保数据的完整性和一致性 事务就是一系列的动作,它们被当作一个单独的工作单元.这些动作要么全部完成,要么全部不起作用 事务的四个关键属性( ...
- Spring中的事务管理详解
在这里主要介绍Spring对事务管理的一些理论知识,实战方面参考上一篇博文: http://www.cnblogs.com/longshiyVip/p/5061547.html 1. 事务简介: 事务 ...
- Spring 中的事务
前言: 之前总结了事务以及数据库中事务相关的知识点,Spring 对于事务做了相应的封装,便于业务开发中使用事务. 项目中使用Spring中的事务首先时基于Mysql数据库中InnoDB 引擎的,如果 ...
随机推荐
- linux mysql 简单记录
mysql 1.linux下启动mysql的命令:mysqladmin start/ect/init.d/mysql start (前面为mysql的安装路径) 2.linux下重启mysql的命令: ...
- 洛谷P1031 均分纸牌
P1031 均分纸牌 题目描述 有 N 堆纸牌,编号分别为 1,2,…, N.每堆上有若干张,但纸牌总数必为 N 的倍数.可以在任一堆上取若干张纸牌,然后移动. 移牌规则为:在编号为 1 堆上取的纸牌 ...
- uoj#213. 【UNR #1】争夺圣杯(单调栈)
传送门 我们枚举每一个元素,用单调栈做两遍计算出它左边第一个大于它的位置\(l[i]\)和右边第一个大于它的位置\(r[i]\),那么一个区间以它为最大值就意味着这个区间的左端点在\([l[i]+1, ...
- 四、python中表示组的概念与定义
现实世界中总是存在一组一组的事物,如俄罗斯方块.游戏中的技能.世界杯总决赛(8个小组,每组4个队) 一.python中如何表示“组”的概念 1.列表 1)定义 [1,2,3,4,5] type[1,2 ...
- SpringBoot2.0 基础案例(12):基于转账案例,演示事务管理操作
本文源码 GitHub地址:知了一笑 https://github.com/cicadasmile/spring-boot-base 一.事务管理简介 1.事务基本概念 一组业务操作ABCD,要么全部 ...
- 两数相加LeetCode
给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字. 如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和 ...
- plsql developer 执行sql 文件
用 Command Window,执行 @'sql file path' 注意,上面sql文件路径要加单引号
- 封装了一个电商放大镜移入放大的功能,适用于VUE
代码地址:https://github.com/zhongqiulan/jqimgzoom 由于vue只支持ie9以上版本,所以这个插件也是一样的 效果图: 第一步,在goodsinfo文件中引入cs ...
- Nginx托管.Net Core应用程序
Nginx托管.Net Core应用程序 一.安装.Net Core 参考官方文档:https://www.microsoft.com/net/core#linuxcentos 1.添加dotnet产 ...
- Redis set(集合)
Redis 的 Set 是 String 类型的无序集合,元素不允许重复. Redis 中集合是通过哈希表实现的,所以添加,删除,查找的复杂度都是 O(1). 集合中最大的元素数为 232 - 1 ( ...