前置文章
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))

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

被折叠的 条评论
为什么被折叠?



