Spring5 源码阅读笔记(4.1)事务切面的 invoke

本文深入探讨了Spring5中事务管理的实现机制,详细解析了从@Transactional注解到TransactionInterceptor的调用流程,以及如何通过ReflectiveMethodInvocation的proceed方法触发事务拦截器,最终完成事务的提交或回滚。

前置文章
Spring5 源码阅读笔记(3.3)JDKDynamicAopProxy 的 invoke 调用
Spring5 源码阅读笔记(4)事务:@EnableTransactionManagement 做了什么?

当我们执行一段打了 @Transactional 的代码时,其实是会走到 JDKDynamicAopProxy 的 invoke 方法的(如果用的是 JDKDynamicAopProxy )。

这时候,chain 里会有一个 TransactionInterceptor,然后会调用 invocation.proceed()。

类 ReflectiveMethodInvocation

@Override
@Nullable
public Object proceed() throws Throwable {
   
   
	//	We start with an index of -1 and increment early.
	//如果执行链中的advice全部执行完,则直接调用joinPoint方法,就是被代理方法
	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)) 
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值