概述

TransactionInterceptorSpring框架内置实现的一个MethodInterceptor,用于声明式事务管理,使用Spring事务基础设施org.springframework.transaction.PlatformTransactionManager

作为一个MethodInterceptor,TransactionInterceptor会被包裹在使用了事务注解的bean组件外面形成该组件的代理对象,当调用相应使用事务注解的方法时,TransactionInterceptor的方法拦截器逻辑会被应用,从而完成相应的事务管理。

TransactionInterceptor继承自TransactionAspectSupport。主要的事务管理逻辑实现在该基类中。TransactionInterceptor自身主要是实现接口MethodInterceptor定义的方法invoke,触发被TransactionAspectSupport事务拦截逻辑包围的目标方法的调用。

关于TransactionInterceptor如何被引入到应用,ProxyTransactionManagementConfiguration是一个很好的例子。在该配置类中,TransactionInterceptor被作为一个bean定义注册到容器,它会被ProxyTransactionManagementConfiguration注册到容器的另外一个bean transactionAdvisor使用;应用启动时Spring的自动代理机制会发现transactionAdvisor以及它所使用的TransactionInterceptor,并将该TransactionInterceptor在创建相应bean的代理对象时包裹到bean外部。

源代码解析

spring-tx-5.1.5.RELEASE

package org.springframework.transaction.interceptor;// 省略 import 行@SuppressWarnings("serial")
public class TransactionInterceptor extends TransactionAspectSupport implements MethodInterceptor, Serializable {/*** Create a new TransactionInterceptor.* <p>Transaction manager and transaction attributes still need to be set.* @see #setTransactionManager* @see #setTransactionAttributes(java.util.Properties)* @see #setTransactionAttributeSource(TransactionAttributeSource)*/public TransactionInterceptor() {}/*** Create a new TransactionInterceptor.* @param ptm the default transaction manager to perform the actual transaction management* @param attributes the transaction attributes in properties format* @see #setTransactionManager* @see #setTransactionAttributes(java.util.Properties)*/public TransactionInterceptor(PlatformTransactionManager ptm, Properties attributes) {setTransactionManager(ptm);setTransactionAttributes(attributes);}/*** Create a new TransactionInterceptor.* @param ptm the default transaction manager to perform the actual transaction management* @param tas the attribute source to be used to find transaction attributes* @see #setTransactionManager* @see #setTransactionAttributeSource(TransactionAttributeSource)*/public TransactionInterceptor(PlatformTransactionManager ptm, TransactionAttributeSource tas) {setTransactionManager(ptm);setTransactionAttributeSource(tas);}// MethodInterceptor 接口所定义的方法@Override@Nullablepublic Object invoke(MethodInvocation invocation) throws Throwable {// Work out the target class: may be {@code null}.// The TransactionAttributeSource should be passed the target class// as well as the method, which may be from an interface.Class<?> targetClass = (invocation.getThis() != null ? AopUtils.getTargetClass(invocation.getThis()) : null);// Adapt to TransactionAspectSupport's invokeWithinTransaction...// 使用事务拦截逻辑包围目标方法的调用,从而起到事务管理的效果// 方法 invokeWithinTransaction 由基类 TransactionAspectSupport 提供实现return invokeWithinTransaction(invocation.getMethod(), targetClass, invocation::proceed);}//---------------------------------------------------------------------// Serialization support//---------------------------------------------------------------------private void writeObject(ObjectOutputStream oos) throws IOException {// Rely on default serialization, although this class itself doesn't carry state anyway...oos.defaultWriteObject();// Deserialize superclass fields.oos.writeObject(getTransactionManagerBeanName());oos.writeObject(getTransactionManager());oos.writeObject(getTransactionAttributeSource());oos.writeObject(getBeanFactory());}private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {// Rely on default serialization, although this class itself doesn't carry state anyway...ois.defaultReadObject();// Serialize all relevant superclass fields.// Superclass can't implement Serializable because it also serves as base class// for AspectJ aspects (which are not allowed to implement Serializable)!setTransactionManagerBeanName((String) ois.readObject());setTransactionManager((PlatformTransactionManager) ois.readObject());setTransactionAttributeSource((TransactionAttributeSource) ois.readObject());setBeanFactory((BeanFactory) ois.readObject());}}

参考文章

  • Spring何时为使用事务注解的bean创建代理 ?
  • Spring AOP : 自动代理创建机制 (APC)
  • Spring Boot 关于 Hibernate JPA 有关的自动配置
  • Spring Transaction : TransactionAspectSupport

Spring Transaction : TransactionInterceptor相关推荐

  1. 重拾-Spring Transaction

    问题 面试中是不是有时经常会被问到 "Spring 事务如何管理的了解吗?" ,"Spring 事务的传播性有哪些,能聊聊它们的使用场景吗?", "事 ...

  2. spring Transaction Management --官方

    原文链接:http://docs.spring.io/spring/docs/current/spring-framework-reference/html/transaction.html 12.  ...

  3. Spring Transaction 使用入门 (转)

    Spring Transaction 使用入门 一.开篇陈述 1.1 写文缘由 最近在系统学习spring框架IoC.AOP.Transaction相关的知识点,准备写三篇随笔记录学习过程中的感悟.这 ...

  4. spring Transaction Propagation 事务传播

    spring Transaction中有一个很重要的属性:Propagation.主要用来配置当前需要执行的方法,与当前是否有transaction之间的关系. 我晓得有点儿抽象,这也是为什么我想要写 ...

  5. SSM:Spring整合MyBatis框架时出现 java.lang.AbstractMethodError: org.mybatis.spring.transaction.SpringManaged

    QUESTION:出现异常:java.lang.AbstractMethodError: org.mybatis.spring.transaction.SpringManagedTransaction ...

  6. Active Spring transaction synchronization or active JTA transaction with specifi

    1,写了一个保存日志方法,在无事务方法体里执行. 2,使用hibernate的save方法保存时报错 java.lang.IllegalStateException: Active Spring tr ...

  7. Spring Transaction : TransactionAspectSupport

    概述 TransactionAspectSupport 是Spring的事务切面逻辑抽象基类,该类实现了事务切面逻辑,但是自身设计为不能被直接使用,而是作为抽象基类被实现子类使用,应用于声明式事务使用 ...

  8. spring transaction源码分析--事务架构

    1. 引言  事务特性 事务是并发控制的单元,是用户定义的一个操作序列.这些操作要么都做,要么都不做,是一个不可分割的工作单位.通过事务将逻辑相关的一组操作绑定在一起,以便服务器 保持数据的完整性.事 ...

  9. Spring-Spring MVC + Spring JDBC + Spring Transaction + Maven 构建web登录模块

    概述 功能简介 环境准备 构建工具Maven 数据库脚本Oracle 建立工程 类包及Spring配置文件规划 持久层 建立领域对象 用户领域对象 登录日志领域对象 UserDao LoginLogD ...

  10. Spring transaction事务之roll back回滚: rollback-for

    试验方法: 写一个单元测试,调用一个service层方法(发生对数据库进行写操作的方法--insert.update.delete)即可. applicationContext.xml 样子(如何设置 ...

最新文章

  1. 0.0 目录-深度学习第三课《结构化机器学习项目》-Stanford吴恩达教授
  2. 代替Excel上载的方法
  3. 一道网易游戏笔试题的不同解法
  4. 暴力子字符串查找算法
  5. DoNet 高效开发必备开发工具
  6. Android 隐藏底部三个虚拟按键
  7. 字符串string 、byte[]、MemoryStream、Base64String的相互转换
  8. python遇到‘\u’开头的unicode编码
  9. ansible 第二次练习
  10. 【sklearn第三讲】数据预处理
  11. 2017-9-19Linux基础知识(2)
  12. CA认证技术的研究与设计
  13. vs code 代码格式化整理
  14. java jbutton 大小_Java swing JButton的顺序和大小
  15. Spring 实战-第六章-渲染Web视图-6.2创建JSP视图
  16. PHP将淘宝客链接转换提取成普通淘宝链接
  17. 关于uni-app入门看完这篇就够了
  18. MinGW安装包下载及下载失败解决
  19. 怎样才能让网站建设效果更加出色
  20. 克隆Centos之后,ifconfig得不到ip地址,mac地址冲突

热门文章

  1. 轮廓检测论文解读 | 整体嵌套边缘检测HED | CVPR | 2015
  2. 5G网络演进行业及市场发展需求
  3. Unity手游实战:从0开始SLG——本地化篇(一)聊聊游戏本地化
  4. 笔记——衡量回归算法的标准最好的评价指标R Square
  5. Smart Game Booster v5.2.0.567 FPS游戏优化加速工具
  6. 阿里云短信服务java发送短信验证码
  7. 给CSDN上原创文章添加版权声明(干货)
  8. vscode SVN not found
  9. 记录一下Material Dialogs的使用
  10. 配置Skype for business 2015混合部署