导包,导入新的约束,通知都不用配了,这两个是一样的,导包这一步,新的约束,tx这些都是一样的,第三步开始不同,这个注解配置大概一分钟就讲完了,第三步咱们可以开启注解,管理事务,那这块的话,咱们的配置文件,这个配置文件再来一份新的吧,在这里把刚才写的,这里开启使用注解管理事务,这里使用的是annotaion-driven,就配这么多就可以了,这个爽,这个就开了,第四步使用注解,使用注解的话看着,咱们要在transfer方法加注解,加一个@Transactional,事务就加上了,isolation等于REPEATABLE_READ,propagation等于REQUIRED,然后逗号还有什么,readOnly等于false,完事了,这样的话事务就配完了,喜欢哪种,这种太多了,咱们试一下成功不成功,接下来咱们转账,咱们怎么试事务成功了没有,把这个打开,这样的话就报错,还是800 1200,咱们的注解事务是不是也OK了,这两种都是可以的,接下来还有个问题,有人的说这个注解细想一下也挺麻烦的,10个方法就要加10个注解,每一个方法加一个注解,如果你觉得这个麻烦的话你可以加载类上,这样的话类中所有的业务方法,都应用这个策略了,下面的某个方法比如read的不是only,readOnly不是false,等于true,那怎么办呢,咱们类上是true,如果不是true你再额外加一个,看明白啥意思不,有的在类上加也可以,在方法上加也可以,看明白啥意思不,这就是咱们的注解,你们喜欢注解的,那这样的话咱们的AOP事务,这两种方式,就讲完了,把今天敲的AOP事务这个,回去以后你们要做任务,你们要把AOP这个配置,你们两种方式,XML配置,和注解配置,这两种分别实验一下,实验成果就算OK,甚至昨天你们AOP都能整完,这个东西敲完你们AOP就OK了,因为学AOP就是为了今天AOP事务,步骤给你们列出来了
<?xml version="1.0" encoding="UTF-8"?>
<beans  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" 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-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd "><!-- 指定spring读取db.properties配置 -->
<context:property-placeholder location="classpath:db.properties"  /><!-- 事务核心管理器,封装了所有事务操作. 依赖于连接池 -->
<bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" ><property name="dataSource" ref="dataSource" ></property>
</bean><!-- 事务模板对象 -->
<bean name="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate" ><property name="transactionManager" ref="transactionManager" ></property>
</bean><!-- 开启使用注解管理aop事务 -->
<tx:annotation-driven/><!-- 1.连接池 -->
<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" ><property name="jdbcUrl" value="${jdbc.jdbcUrl}" ></property><property name="driverClass" value="${jdbc.driverClass}" ></property><property name="user" value="${jdbc.user}" ></property><property name="password" value="${jdbc.password}" ></property>
</bean><!-- 2.Dao-->
<bean name="accountDao" class="com.learn.dao.AccountDaoImpl" ><property name="dataSource" ref="dataSource" ></property>
</bean>
<!-- 3.Service-->
<bean name="accountService" class="com.learn.service.AccountServiceImpl" ><property name="ad" ref="accountDao" ></property><property name="tt" ref="transactionTemplate" ></property>
</bean>  </beans>
package com.learn.dao;public interface AccountDao {//加钱void increaseMoney(Integer id,Double money);//减钱void decreaseMoney(Integer id,Double money);
}
package com.learn.dao;import org.springframework.jdbc.core.support.JdbcDaoSupport;public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao  {@Overridepublic void increaseMoney(Integer id, Double money) {getJdbcTemplate().update("update t_account set money = money+? where id = ? ", money,id);}@Overridepublic void decreaseMoney(Integer id, Double money) {getJdbcTemplate().update("update t_account set money = money-? where id = ? ", money,id);}}
package com.learn.service;public interface AccountService {//转账方法void transfer(Integer from,Integer to,Double money);}
package com.learn.service;import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionTemplate;import com.learn.dao.AccountDao;@Transactional(isolation=Isolation.REPEATABLE_READ,propagation=Propagation.REQUIRED,readOnly=true)
public class AccountServiceImpl implements AccountService {private AccountDao ad ;private TransactionTemplate tt;@Override@Transactional(isolation=Isolation.REPEATABLE_READ,propagation=Propagation.REQUIRED,readOnly=false)public void transfer(final Integer from,final Integer to,final Double money) {//减钱ad.decreaseMoney(from, money);int i = 1/0;//加钱ad.increaseMoney(to, money);}/*  @Overridepublic void transfer(final Integer from,final Integer to,final Double money) {tt.execute(new TransactionCallbackWithoutResult() {@Overrideprotected void doInTransactionWithoutResult(TransactionStatus arg0) {//减钱ad.decreaseMoney(from, money);int i = 1/0;//加钱ad.increaseMoney(to, money);}});}
*/public void setAd(AccountDao ad) {this.ad = ad;}public void setTt(TransactionTemplate tt) {this.tt = tt;}}
package com.learn.tx;import javax.annotation.Resource;import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import com.learn.service.AccountService;@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class Demo {@Resource(name="accountService")private AccountService as;@Testpublic void fun1(){as.transfer(1, 2, 100d);}
}
jdbc.jdbcUrl=jdbc:mysql:///day20
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.user=root
jdbc.password=123456

spring事务管理-注解配置aop事务(重点)相关推荐

  1. spring事务管理-xml配置aop事务(重点)

    刚才咱们是使用了模板操作咱们事务,当然使用模板操作比较low,还得写代码,每个方法都写太费劲了,首先把之前写的先注释掉,把这个transfer这个代码直接复制一份,然后底下展一份,留着注释掉就行了,上 ...

  2. Spring MVC 中使用AOP 进行事务管理--XML配置实现

    1.今天写一篇使用AOP进行事务管理的示例,关于事务首先需要了解以下几点 (1)事务的特性 原子性(Atomicity):事务是一个原子操作,由一系列动作组成.事务的原子性确保动作要么全部完成,要么完 ...

  3. Spring JDBC-使用注解配置声明式事务

    系列 概述 使用Transactional注解 txannotation-driven其他属性 关于Transaction的属性 在何处标注Transactional注解 在方法处使用注解 使用不同的 ...

  4. Spring事务管理器配置两种配置方法,使用方法

    配置事务管理器 编程式事务管理: 要修改原来的代码,加入事务管理代码 (侵入性 )- 不推荐,不使用 声明式事务管理:底层就是AOP的环绕通知, - 推荐 用XML配置方式添加事务管理(tx.aop约 ...

  5. 注解mysql事物管理_Spring事务管理-注解版

    Spring事务管理-注解版 一.拷贝必要的jar包到工程的lib目录 二.创建spring的配置文件并导入约束 xmlns:xsi="http://www.w3.org/2001/XMLS ...

  6. Spring 4 xml 注解配置谅解 spring

    2019独角兽企业重金招聘Python工程师标准>>> Spring 4 xml 注解配置谅解 博客分类: spring <Spring in Action>4th Ed ...

  7. 事务管理学习笔记:事务特性与常见并发异常

    事务管理笔记 什么是事务? 事务是由N步数据库操作序列组成的逻辑执行单元,这系列操作要么全执行,要么全不执行. 事务特性 原子性:事务是应用中不可再分的最下执行体 一致性:事务的执行结果,必须数据从一 ...

  8. Spring声明式事务管理的配置详解

    环境配置 项目使用SSH架构,现在要添加Spring事务管理功能,针对当前环境,只需要添加Spring 2.0 AOP类库即可.添加方法: 点击项目右键->Build Path->Add ...

  9. 全面分析 Spring 的编程式事务管理及声明式事务管理(转)

    摘要 Spring 的事务管理是 Spring 框架中一个比较重要的知识点,该知识点本身并不复杂,只是由于其比较灵活,导致初学者很难把握.本教程从基础知识开始,详细分析了 Spring 事务管理的使用 ...

最新文章

  1. python:dataframe
  2. ORM数据层框架的设计热点:更新指定的列的几种设计方案
  3. linux命令last格式,Linux中aulast命令起什么作用呢?
  4. angular2子组件的事件传递(任意组件事件传递)
  5. 【DP】饥饿的WZK(jzoj 1998)
  6. linux 取消混杂模式,Linux下网卡混杂模式设置和取消
  7. linux服务器时间乱码问题解决
  8. AFTER触发器与INSTEAD OF触发器的区别
  9. 深度学习发展与机器学习
  10. 第一章 硬件介绍和环境配置
  11. 寒流来袭·《求职初体验》
  12. 国产机GSM系列手机常见芯片方案介绍
  13. 通过FISH和下一代测序检测肺腺癌ALK基因融合比较
  14. 第一章 | 加州房价数据集 | 端到端的机器学习 | 回归问题 | tensorflow2.6+sklearn | 学习笔记
  15. html5倒计时效果,html5+css3进度条倒计时动画特效代码【推荐】
  16. 计算机专业简历文案,文案创意求职简历范文
  17. jsp使用session出现The server encountered an unexpected condition that prevented it from fulfilling the r
  18. Mac Safari 此连接非私人连接
  19. mysql配置文件参数详解_MySQL配置文件mysql.ini参数详解
  20. Python:for循环语句

热门文章

  1. Eclipse Plug-in Hello world
  2. Ubuntu gnome 14.10下MySQLdb安装
  3. BIO和NIO的区别
  4. 「Luogu1552」[APIO2012]派遣
  5. 仓库管理的5S如何在仓库中实施
  6. 个人作业——软件产品分析
  7. 轻松为Windows系统快速配置多个网关
  8. 查看Linux系统架构类型的5条常用命令
  9. PHP函数操作数组(集合贴)
  10. WinForm中的各种对话框