Spring Transaction : TransactionInterceptor

本文深入解析Spring框架中的TransactionInterceptor,一种用于声明式事务管理的MethodInterceptor。作为TransactionAspectSupport的子类,它通过实现MethodInterceptor的invoke方法,在使用事务注解的bean组件上创建代理,实现事务管理。文章详细介绍了其构造方法、工作原理及源代码分析。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

概述

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
	@Nullable
	public 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());
	}

}

参考文章

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值