spring tx @Transactional 详解 `Advisor`、`Target`、`ProxyFactory

news/2024/7/8 9:20:44 标签: spring, 事务, aop

在Spring中,@Transactional注解的处理涉及到多个关键组件,包括AdvisorTargetProxyFactory等。下面是详细的解析和代码示例,解释这些组件是如何协同工作的。

1. 关键组件介绍

1.1 Advisor

Advisor是一个Spring AOP的概念,它包含了切点(Pointcut)和通知(Advice)。在事务管理中,TransactionAttributeSourceAdvisor是一个典型的Advisor。

1.2 Target

Target是指被代理的目标对象,即实际执行业务逻辑的对象。

1.3 ProxyFactory

ProxyFactory是Spring提供的用于创建代理对象的工厂类。它可以使用JDK动态代理或CGLIB创建代理对象。

2. @Transactional的处理流程

  1. 解析注解:Spring扫描@Transactional注解。
  2. 创建Advisor:创建包含事务处理逻辑的Advisor
  3. 创建代理对象:使用ProxyFactory为目标对象创建代理对象,并将Advisor加入到代理对象中。

3. 代码示例

3.1 配置类

首先,通过@EnableTransactionManagement启用事务管理。

import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@EnableTransactionManagement
public class AppConfig {
    // DataSource, EntityManagerFactory, TransactionManager beans configuration
}
3.2 目标对象和接口

定义一个业务接口和其实现类:

public interface MyService {
    void myTransactionalMethod();
}

@Service
public class MyServiceImpl implements MyService {
    @Override
    @Transactional
    public void myTransactionalMethod() {
        // 业务逻辑
        System.out.println("Executing myTransactionalMethod");
    }
}
3.3 ProxyFactory和Advisor

使用ProxyFactoryTransactionAttributeSourceAdvisor来创建代理对象并处理事务

import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.transaction.interceptor.TransactionInterceptor;
import org.springframework.transaction.interceptor.TransactionAttributeSource;
import org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource;

public class ProxyFactoryExample {
    public static void main(String[] args) {
        // 创建目标对象
        MyService target = new MyServiceImpl();

        // 创建事务属性源
        TransactionAttributeSource transactionAttributeSource = new NameMatchTransactionAttributeSource();

        // 创建事务拦截器
        TransactionInterceptor transactionInterceptor = new TransactionInterceptor();
        transactionInterceptor.setTransactionAttributeSource(transactionAttributeSource);

        // 创建Advisor
        DefaultPointcutAdvisor advisor = new DefaultPointcutAdvisor();
        advisor.setAdvice(transactionInterceptor);

        // 创建ProxyFactory
        ProxyFactory proxyFactory = new ProxyFactory();
        proxyFactory.setTarget(target);
        proxyFactory.addAdvisor(advisor);

        // 创建代理对象
        MyService proxy = (MyService) proxyFactory.getProxy();

        // 调用代理对象的方法
        proxy.myTransactionalMethod();
    }
}

4. 详细解释

4.1 创建目标对象
MyService target = new MyServiceImpl();

这是被代理的目标对象,它包含了业务逻辑,并使用了@Transactional注解。

4.2 创建事务属性源
TransactionAttributeSource transactionAttributeSource = new NameMatchTransactionAttributeSource();

TransactionAttributeSource用于解析事务属性。NameMatchTransactionAttributeSource是一个实现类,可以基于方法名称匹配事务属性。

4.3 创建事务拦截器
TransactionInterceptor transactionInterceptor = new TransactionInterceptor();
transactionInterceptor.setTransactionAttributeSource(transactionAttributeSource);

TransactionInterceptor实现了MethodInterceptor接口,用于在方法调用前后处理事务逻辑。

4.4 创建Advisor
DefaultPointcutAdvisor advisor = new DefaultPointcutAdvisor();
advisor.setAdvice(transactionInterceptor);

DefaultPointcutAdvisor包含了事务拦截器,可以在匹配的方法上应用事务逻辑。

4.5 创建ProxyFactory
ProxyFactory proxyFactory = new ProxyFactory();
proxyFactory.setTarget(target);
proxyFactory.addAdvisor(advisor);

ProxyFactory用于创建代理对象。它将目标对象和Advisor结合起来,生成代理对象。

4.6 创建代理对象并调用方法
MyService proxy = (MyService) proxyFactory.getProxy();
proxy.myTransactionalMethod();

通过ProxyFactory.getProxy()方法创建代理对象,并调用代理对象的方法。这时,事务拦截器会在方法调用前后执行事务处理逻辑。

5. 总结

通过以上代码示例,可以看出Spring如何解析@Transactional注解,并使用AdvisorTargetProxyFactory创建代理对象来处理事务逻辑。这些组件协同工作,实现了自动的事务管理。

proxyFactory源码

targetSource与代理对象是两个对象,
在调用代理对象的时候,实际上是要被DynamicAdvisedInterceptor拦截,之后在拦截方法中执行执行拦截器调用链,并把targetSource传给拦截器。
相当于把targetSource对象(非类)作为成员变量传递给代理对象,然后对targetSource对象的方法调用增强。
类->BeanDefinition->bean初始化->为bean添加后置处理器,替换bean对象为proxy对象,其中bean对象作为代理对象的成员变量targetSource,代理对象通过在拦截方法中对targetSource对象的方法调用前后执行advisor的方法。

对比 @Configuration对@bean方法的处理,是直接生成子类,子类重写@bean方法,子类作为BeanDefinition替换原始@Configuration注解的类, 因此在子类中的this调用就是对子类重写的方法调用。

//CglibAopProxy
/**
	 * General purpose AOP callback. Used when the target is dynamic or when the
	 * proxy is not frozen.
	 */
	private static class DynamicAdvisedInterceptor implements MethodInterceptor, Serializable {

		private final AdvisedSupport advised;

		public DynamicAdvisedInterceptor(AdvisedSupport advised) {
			this.advised = advised;
		}

		@Override
		@Nullable
		public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
			Object oldProxy = null;
			boolean setProxyContext = false;
			Object target = null;
			TargetSource targetSource = this.advised.getTargetSource();
			try {
				if (this.advised.exposeProxy) {
					// Make invocation available if necessary.
					oldProxy = AopContext.setCurrentProxy(proxy);
					setProxyContext = true;
				}
				// Get as late as possible to minimize the time we "own" the target, in case it comes from a pool...
				target = targetSource.getTarget();
				Class<?> targetClass = (target != null ? target.getClass() : null);
				List<Object> chain = this.advised.getInterceptorsAndDynamicInterceptionAdvice(method, targetClass);
				Object retVal;
				// Check whether we only have one InvokerInterceptor: that is,
				// no real advice, but just reflective invocation of the target.
				if (chain.isEmpty() && CglibMethodInvocation.isMethodProxyCompatible(method)) {
					// We can skip creating a MethodInvocation: just invoke the target directly.
					// Note that the final invoker must be an InvokerInterceptor, so we know
					// it does nothing but a reflective operation on the target, and no hot
					// swapping or fancy proxying.
					Object[] argsToUse = AopProxyUtils.adaptArgumentsIfNecessary(method, args);
					retVal = invokeMethod(target, method, argsToUse, methodProxy);
				}
				else {
					// We need to create a method invocation...
					// 链式调用
					retVal = new CglibMethodInvocation(proxy, target, method, args, targetClass, chain, methodProxy).proceed();
				}
				retVal = processReturnType(proxy, target, method, retVal);
				return retVal;
			}
			finally {
				if (target != null && !targetSource.isStatic()) {
					targetSource.releaseTarget(target);
				}
				if (setProxyContext) {
					// Restore old proxy.
					AopContext.setCurrentProxy(oldProxy);
				}
			}
		}

		@Override
		public boolean equals(@Nullable Object other) {
			return (this == other ||
					(other instanceof DynamicAdvisedInterceptor &&
							this.advised.equals(((DynamicAdvisedInterceptor) other).advised)));
		}

		/**
		 * CGLIB uses this to drive proxy creation.
		 */
		@Override
		public int hashCode() {
			return this.advised.hashCode();
		}
	}

//ReflectiveMethodInvocation
@Override
	@Nullable
	public Object proceed() throws Throwable {
		// We start with an index of -1 and increment early.
		if (this.currentInterceptorIndex == this.interceptorsAndDynamicMethodMatchers.size() - 1) {
			return invokeJoinpoint();
		}

		Object interceptorOrInterceptionAdvice =
				this.interceptorsAndDynamicMethodMatchers.get(++this.currentInterceptorIndex);
		if (interceptorOrInterceptionAdvice instanceof InterceptorAndDynamicMethodMatcher) {
			// Evaluate dynamic method matcher here: static part will already have
			// been evaluated and found to match.
			InterceptorAndDynamicMethodMatcher dm =
					(InterceptorAndDynamicMethodMatcher) interceptorOrInterceptionAdvice;
			Class<?> targetClass = (this.targetClass != null ? this.targetClass : this.method.getDeclaringClass());
			if (dm.methodMatcher.matches(this.method, targetClass, this.arguments)) {
				return dm.interceptor.invoke(this);
			}
			else {
				// Dynamic matching failed.
				// Skip this interceptor and invoke the next in the chain.
				return proceed();
			}
		}
		else {
			// It's an interceptor, so we just invoke it: The pointcut will have
			// been evaluated statically before this object was constructed.
			//调用 @Transactional事务拦截器
			return ((MethodInterceptor) interceptorOrInterceptionAdvice).invoke(this);
		}
	}
//TransactionInterceptor
   @Nullable
    public Object invoke(MethodInvocation invocation) throws Throwable {
        Class<?> targetClass = invocation.getThis() != null ? AopUtils.getTargetClass(invocation.getThis()) : null;
        return this.invokeWithinTransaction(invocation.getMethod(), targetClass, new CoroutinesInvocationCallback() {
            @Nullable
            public Object proceedWithInvocation() throws Throwable {
            // 拦截器链式调用
                return invocation.proceed();
            }

            public Object getTarget() {
                return invocation.getThis();
            }

            public Object[] getArguments() {
                return invocation.getArguments();
            }
        });
    }

拦截器链式调用


http://www.niftyadmin.cn/n/5536962.html

相关文章

OpenStack开源虚拟化平台(一)

目录 一、OpenStack背景介绍&#xff08;一&#xff09;OpenStack是什么&#xff08;二&#xff09;OpenStack的主要服务 二、计算服务Nova&#xff08;一&#xff09;Nova组件介绍&#xff08;二&#xff09;Libvirt简介&#xff08;三&#xff09;Nova中的RabbitMQ解析 OpenS…

Anaconda安装及配置+pytorch深度学习环境(2024复旦计算机工作站0705)

目录 前言 &#xff08;补充&#xff1a;四、安装GPU环境的pytorch&#xff09; 正文 一、Pytorch 二、Tensor 三、CUDA 四、其他技巧 五、数据 六、torch.nn 前言 深度学习越来越火啦&#xff0c;深入到各行各业&#xff0c;小北个人也对深度学习很感兴趣&#xff0…

推荐算法学习笔记2.1:基于深度学习的推荐算法-基于共线矩阵的深度推荐算法-NeuralCF模型

NeuralCF模型 NeuralCF模型将矩阵分解和逻辑回归思想进行结合&#xff0c;利用神经网络分别学习用户和物品的隐向量表示&#xff08;Embedding&#xff09;&#xff0c;然后将矩阵分解中的内积互操作替换成神经网络计算&#xff0c;从而更好地从特征中学习到有用的信息。 原论…

element el-table表格切换分页保留分页数据+限制多选数量

el-table表格并没有相关的方法来禁用表头里面的多选按钮 那么我们可以另辟蹊径&#xff0c;来实现相同的多选切换分页&#xff08;保留分页数据&#xff09; 限制多选数量的效果 <el-table:data"tableData"style"width: 100%">// 不使用el-talbe自带…

Linux arm板

NXP_IMX8MINI调试笔记 ARM板挂载PC端的文件夹 ARM板挂载PC端的文件夹 NFS网络挂载文件夹&#xff0c;用于文件传输 #192.168.1.11为PC端的IP地址 mount -t nfs -o nolock,nfsvers3 192.168.1.11:/home/yl/nfs nfs/静态IP永久设置 #在/etc/rc.local文件中添加 ifconfig eth0 …

JavaScript(6)——数据类型转换

为什么需要类型转换&#xff1f; JavaScript是弱数据类型&#xff1a;JavaScript不知道变量到底属于哪种数据类型&#xff0c;只有赋值了才清除 使用表单&#xff0c;prompt获取的数据默认为字符串类型&#xff0c;此时不能直接进行算数运算 隐式转换 某些运算符被执行时&am…

富格林:阻止欺诈落实盈利出金

富格林认为&#xff0c;现货黄金作为一种国际性的投资产品&#xff0c;价格在各种因素的综合影响下不断地波动变化&#xff0c;为投资者带来了获利出金机会的同时也伴随着一定的欺诈风险&#xff0c;想要落实盈利出金计划&#xff0c;还需要掌握正确的投资技巧。以下是富格林总…

Lesson 48 Do you like ... ? Do you want ... ?

Lesson 48 Do you like … ? Do you want … ? 词汇 fresh a. 新鲜的【食物】 搭配&#xff1a;fresh water 淡水    fresh man 新生    fresh air 新鲜空气    fresh egg 新鲜鸡蛋 例句&#xff1a;我们喜欢新鲜的空气。    We like fresh egg. egg n. 蛋【通…