前置知识
JDK 动态代理
关联知识 CglibAopProxy
JdkDynamicAopProxy 主要执行的地方是 InvocationHandler(JDK动态代理的处理接口)的invoker方法。
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { // code}
invoker方法里最主要的地方就是先判断方法是不是特殊的方法, equal hashCode。 还有类是不是这俩个特殊类。
if (!this.equalsDefined && AopUtils.isEqualsMethod(method)) {
// The target does not implement the equals(Object) method itself.
return equals(args[0]);
}
else if (!this.hashCodeDefined && AopUtils.isHashCodeMethod(method)) {
// The target does not implement the hashCode() method itself.
return hashCode();
}
else if (method.getDeclaringClass() == DecoratingProxy.class) {
// There is only getDecoratedClass() declared -> dispatch to proxy config.
return AopProxyUtils.ultimateTargetClass(this.advised);
}
else if (!this.advised.opaque && method.getDeclaringClass().isInterface() &&
method.getDeclaringClass().isAssignableFrom(Advised.class)) {
// Service invocations on ProxyConfig with the proxy config...
return AopUtils.invokeJoinpointUsingReflection(this.advised, method, args);
}
==再去获取方法的拦截器的调用链 ==
// todo 多会去debug看下各种业务场景,调用链里都有啥。比如WEB场景、JPA 取数据场景、@Transactional场景这几个经典场景。
List<Object> chain = this.advised.getInterceptorsAndDynamicInterceptionAdvice(method, targetClass);
拦截器的调用链为空直接执行,不为空就用Spring 特别吊的MethodInvocation来处理了
MethodInvocation invocation =
new ReflectiveMethodInvocation(proxy, target, method, args, targetClass, chain);
// Proceed to the joinpoint through the interceptor chain.
retVal = invocation.proceed();
解决疑问
拦截器的调用链 哪里来的,哪里注入的。参考Spring系列之AOP(4)——JdkDynamicAopProxy与CglibAopProxy
答 : 它将Advice通知适配成Spring预先设计好的拦截器。适配和注册的工作是在GlobalAdvisorAdapterRegistry的getInterceptors()中完成的。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/76458.html