在Spring AOP中代理对象增强通知执行原理一文中我们提到了为目标方法进行增强分析了其执行流程。本文我们详细研究一下拦截器链的产生。
取得拦截器链的工作是由配置好的advisorChainFactory来完成的,从名字上可以猜到,它是一个生成通知器链的工厂。在这里advisorchainFactory被配置成一个DefaultAdvisorChainFactory对象,实现了interceptor链的获取。
拦截器链的产生是由DefaultAdvisorChainFactory
类完成的,该类如下所示。
public class DefaultAdvisorChainFactory implements AdvisorChainFactory, Serializable { @Override public List<Object> getInterceptorsAndDynamicInterceptionAdvice( Advised config, Method method, @Nullable Class<?> targetClass) { // This is somewhat tricky... We have to process introductions first, // but we need to preserve order in the ultimate list. // 单例 AdvisorAdapterRegistry AdvisorAdapterRegistry registry = GlobalAdvisorAdapterRegistry.getInstance(); //获取到Advisors 每个advisor包含了advice Advisor[] advisors = config.getAdvisors(); List<Object> interceptorList = new ArrayList<>(advisors.length); // 获取target class Class<?> actualClass = (targetClass != null ? targetClass : method.getDeclaringClass()); // 是否有引入通知 Boolean hasIntroductions = null; // 遍历advisors for (Advisor advisor : advisors) { // 1.判断是否为PointcutAdvisor if (advisor instanceof PointcutAdvisor) { // Add it conditionally. PointcutAdvisor pointcutAdvisor = (PointcutAdvisor) advisor; if (config.isPreFiltered() || pointcutAdvisor.getPointcut().getClassFilter().matches(actualClass)) { MethodMatcher mm = pointcutAdvisor.getPointcut().getMethodMatcher(); boolean match; if (mm instanceof IntroductionAwareMethodMatcher) { if (hasIntroductions == null) { hasIntroductions = hasMatchingIntroductions(advisors, actualClass); } match = ((IntroductionAwareMethodMatcher) mm).matches(method, actualClass, hasIntroductions); } else { match = mm.matches(method, actualClass); } if (match) { MethodInterceptor[] interceptors = registry.getInterceptors(advisor); 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)); } } } } // 2.判断是否为引入通知IntroductionAdvisor 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)); } } // 3.其他则直接获取Interceptor else { Interceptor[] interceptors = registry.getInterceptors(advisor); interceptorList.addAll(Arrays.asList(interceptors)); } } return interceptorList; } // 是否包含引入通知且适配actualClass private static boolean hasMatchingIntroductions(Advisor[] advisors, Class<?> actualClass) { for (Advisor advisor : advisors) { if (advisor instanceof IntroductionAdvisor) { IntroductionAdvisor ia = (IntroductionAdvisor) advisor; if (ia.getClassFilter().matches(actualClass)) { return true; } } } return false; } }
从上述方法我们可以了解到其首先获取了AdvisorAdapterRegistry与advisors,然后对advisors进行了遍历最终通过AdvisorAdapterRegistry获取Interceptor。
在对advisors进行遍历获取Interceptor分为三种情况:
PointcutAdvisor,如我们自定义切面中的advice被包装后的advisor
IntroductionAdvisor,如DefaultIntroductionAdvisor与DeclareParentsAdvisor
其他如ProxyFactoryBean的内部类PrototypePlaceholderAdvisor
【1】PointcutAdvisor情况下获取Interceptor
如果Advisor是PointcutAdvisor,那么将会执行如下方法逻辑。这种Advisor也是最常见的Advisor,我们切面中的增强方法就会被包装为这种类型的Advisor。
// 类型转换 PointcutAdvisor pointcutAdvisor = (PointcutAdvisor) advisor; // 首先通过ClassFilter进行类型匹配 if (config.isPreFiltered() || pointcutAdvisor.getPointcut().getClassFilter().matches(actualClass)) { // 我们自定义切面这里的Pointcut是AspectJExpressionPointcut MethodMatcher mm = pointcutAdvisor.getPointcut().getMethodMatcher(); boolean match; // 其次进行方法匹配,是否对actualClass的method生效 if (mm instanceof IntroductionAwareMethodMatcher) { // 这里会判断是否为IntroductionAwareMethodMatcher, //我们的AspectJExpressionPointcut就是这种类型 if (hasIntroductions == null) { // 是否有引入通知且匹配当前actualClass hasIntroductions = hasMatchingIntroductions(advisors, actualClass); } match = ((IntroductionAwareMethodMatcher) mm).matches(method, actualClass, hasIntroductions); } else { match = mm.matches(method, actualClass); } // 如果匹配则调用registry获取MethodInterceptor if (match) { MethodInterceptor[] interceptors = registry.getInterceptors(advisor); 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)); } } }
如上所示,首先获取MethodMatcher 校验当前advisor是否匹配actualClass的该method,如果匹配则调用registry获取MethodInterceptor[]。如果MethodMatcher 是在runTime状态(也就是动态的)则对之前拿到的MethodInterceptor[] interceptors进行遍历封装为InterceptorAndDynamicMethodMatcher。可以看到,在DefaultAdvisorChainFactory的实现中,首先够早了一个GlobalAdvisorAdapterRegistry单件,然后对配置的Advisor通知器进行逐个遍历,在遍历的过程中会使用GlobalAdvisorAdapterRegistry完成适配和注册过程。
【2】IntroductionAdvisor情况下获取Interceptor
IntroductionAdvisor其实就是指DefaultIntroductionAdvisor和DeclareParentsAdvisor。方法如下所示,对cofig进行过滤判断及ClassFilter是否匹配actualClass判断后直接使用registry获取Interceptor。
IntroductionAdvisor ia = (IntroductionAdvisor) advisor; if (config.isPreFiltered() || ia.getClassFilter().matches(actualClass)) { Interceptor[] interceptors = registry.getInterceptors(advisor); interceptorList.addAll(Arrays.asList(interceptors)); }
至于最后一种其他情况则是直接通过registry获取Interceptor。
else { Interceptor[] interceptors = registry.getInterceptors(advisor); interceptorList.addAll(Arrays.asList(interceptors)); }
【3】AdvisorAdapterRegistry获取拦截器
这里的AdvisorAdapterRegistry其实是DefaultAdvisorAdapterRegistry。其在实例化的时候就会注册三个Adapter用来对BeforeAdvice、ReturningAdvice及ThrowsAdvice进行适配。
public DefaultAdvisorAdapterRegistry() { registerAdvisorAdapter(new MethodBeforeAdviceAdapter()); registerAdvisorAdapter(new AfterReturningAdviceAdapter()); registerAdvisorAdapter(new ThrowsAdviceAdapter()); }
我们继续看其getInterceptors方法。
@Override public MethodInterceptor[] getInterceptors(Advisor advisor) throws UnknownAdviceTypeException { List<MethodInterceptor> interceptors = new ArrayList<>(3); // 获取Advisor内部的advice Advice advice = advisor.getAdvice(); // 如果其是MethodInterceptor类型,则直接放入 if (advice instanceof MethodInterceptor) { interceptors.add((MethodInterceptor) advice); } // 否则通过适配器尝试得到MethodInterceptor for (AdvisorAdapter adapter : this.adapters) { if (adapter.supportsAdvice(advice)) { interceptors.add(adapter.getInterceptor(advisor)); } } // 如果拦截器为空,抛出异常 if (interceptors.isEmpty()) { throw new UnknownAdviceTypeException(advisor.getAdvice()); } //返回 return interceptors.toArray(new MethodInterceptor[0]); }
如下图所示,AspectJAroundAdvice、AspectJAfterThrowingAdvice以及AspectJAfterAdvice本身就实现了MethodInterceptor接口,是MethodInterceptor类型。
ThrowsAdviceAdapter
// 判断条件 advice instanceof ThrowsAdvice // 返回结果 new ThrowsAdviceInterceptor(advisor.getAdvice())
AfterReturningAdviceAdapter
// 判断条件 advice instanceof AfterReturningAdvice // 返回结果 new AfterReturningAdviceInterceptor(advice)
MethodBeforeAdviceAdapter
// 判断条件 advice instanceof MethodBeforeAdvice // 返回结果 new MethodBeforeAdviceInterceptor(advice)
最终我们获取的拦截器会放到AdvisedSupport的Map<MethodCacheKey, List<Object>> methodCache
缓存里面,如下图所示。