为了账号安全,请及时绑定邮箱和手机立即绑定

Spring源码深度分析问题:Aop实现分别是JdkDynamicAopProxy和 CglibAopProxy

Spring源码深度分析问题:Aop实现分别是JdkDynamicAopProxy和 CglibAopProxy

繁花不似锦 2019-01-17 13:43:46
在invoke方法中有如下代码: // Get the interception chain for this method. List<Object> chain = this.advised.getInterceptorsAndDynamicInterceptionAdvice(method, targetClass); 上面代码也就是获取拦截器链,我的理解是这样 上面这个方法最终调用了如下实现 @SuppressWarnings("serial") public class DefaultAdvisorChainFactory implements AdvisorChainFactory, Serializable { @Override public List<Object> getInterceptorsAndDynamicInterceptionAdvice( Advised config, Method method, Class<?> targetClass) { // This is somewhat tricky... We have to process introductions first, // but we need to preserve order in the ultimate list. List<Object> interceptorList = new ArrayList<Object>(config.getAdvisors().length); Class<?> actualClass = (targetClass != null ? targetClass : method.getDeclaringClass()); boolean hasIntroductions = hasMatchingIntroductions(config, actualClass); AdvisorAdapterRegistry registry = GlobalAdvisorAdapterRegistry.getInstance(); for (Advisor advisor : config.getAdvisors()) { if (advisor instanceof PointcutAdvisor) { // Add it conditionally. PointcutAdvisor pointcutAdvisor = (PointcutAdvisor) advisor; if (config.isPreFiltered() || pointcutAdvisor.getPointcut().getClassFilter().matches(actualClass)) { MethodInterceptor[] interceptors = registry.getInterceptors(advisor); MethodMatcher mm = pointcutAdvisor.getPointcut().getMethodMatcher(); if (MethodMatchers.matches(mm, method, actualClass, hasIntroductions)) { if (mm.isRuntime()) { // Creating a new object instance in the getInterceptors() method // isn't a problem as we normally cache created chains. for (MethodInterceptor interceptor : interceptors) { interceptorList.add(new InterceptorAndDynamicMethodMatcher(interceptor, mm)); } } else { interceptorList.addAll(Arrays.asList(interceptors)); } } } } else if (advisor instanceof IntroductionAdvisor) { IntroductionAdvisor ia = (IntroductionAdvisor) advisor; if (config.isPreFiltered() || ia.getClassFilter().matches(actualClass)) { Interceptor[] interceptors = registry.getInterceptors(advisor); interceptorList.addAll(Arrays.asList(interceptors)); } } else { Interceptor[] interceptors = registry.getInterceptors(advisor); interceptorList.addAll(Arrays.asList(interceptors)); } } return interceptorList; } } 我的问题: 在DefaultAdvisorChainFactory 的getInterceptorsAndDynamicInterceptionAdvice方法中 for循环内 分了三种情况 if (advisor instanceof PointcutAdvisor) else if (advisor instanceof IntroductionAdvisor) 以及 else { Interceptor[] interceptors = registry.getInterceptors(advisor); interceptorList.addAll(Arrays.asList(interceptors)); } 我不理解这样区分的逻辑是什么,为什么要这样区分处理,有朋友可以 解释一下吗,非常感谢### 问题描述 问题出现的环境背景及自己尝试过哪些方法 相关代码 // 请把代码文本粘贴到下方(请勿用图片代替代码) 你期待的结果是什么?实际看到的错误信息又是什么?
查看完整描述

1 回答

?
慕莱坞森

TA贡献1810条经验 获得超4个赞

因为Advisor这个接口有PointcutAdvisor和IntroductionAdvisor两个子接口和一个静态内部实现类PrototypePlaceholderAdvisor,所以这样判断。

查看完整回答
反对 回复 2019-02-12
  • 1 回答
  • 0 关注
  • 730 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信