共计 2146 个字符,预计需要花费 6 分钟才能阅读完成。
Spring Boot 如果一个 service @Transactional 所在办法不是 public 会产生什么?(二)这篇文章里咱们晓得了,no-public 的帽子办法 (其实办法就是 @Transaction 被润饰 ^_^) 最终执行的实例并不是代理类,那么在什么时候代理类被替换掉的呢?
图 1
刚开始执行时,的确是代理类~
图 2
咱们剖析一下 DynamicAdvisedInterceptor.intercept():
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;
// 这里判断指标办法是不是 public
if (chain.isEmpty() && Modifier.isPublic(method.getModifiers())) {
// 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 = methodProxy.invoke(target, argsToUse);
}
else {
// 因为不是 public, 须要执行这里
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);
}
}
}
最终调 CglibAopProxy.invokeJoinpoint();
图 3
@Override
protected Object invokeJoinpoint() throws Throwable {
// 因为咱们的办法不是 public
if (this.publicMethod) {return this.methodProxy.invoke(this.target, this.arguments);
}
else {
// 所有执行这里的逻辑
return super.invokeJoinpoint();}
}
其实就是 ReflectiveMethodInvocation.invokeJoinpoint();
protected Object invokeJoinpoint() throws Throwable {
//
return AopUtils.invokeJoinpointUsingReflection(this.target, this.method, this.arguments);
}
上面是 target,method,arguments 具体的值:
AopUtils.invokeJoinpointUsingReflection()的外围逻辑是 method.invoke(target, args),这样一来就通过非代理类,来进行调用了!
正文完
发表至: springboot
2022-06-03