Spring Boot 如果一个service @Transactional所在方法不是public会发生什么?(二)这篇文章里我们知道了,no-public 的帽子方法(其实方法就是@Transactional被修饰^_^)最终执行的实例并不是代理类,那么在什么时候代理类被替换掉的呢?
图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),这样一来就通过非代理类,来进行调用了!
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。