前提:
https://blog.youkuaiyun.com/convict_eva/article/details/81084833
https://blog.youkuaiyun.com/convict_eva/article/details/81101432
前两篇分析了aop 两种方式实现的大致流程和方式,在这两种实现方式中都有一个很重要的方法获取拦截器链
List<Object> chain = this.advised.getInterceptorsAndDynamicInterceptionAdvice(method, targetClass);
所有的aop增强方法都封装在一个个拦截器中,然后根据这个拦截器链的调用进行增强。
此方法的实现是在advised 对象实现的(advised 是 AdvisedSupport 对象的实例,ProxyFactoryBean 是AdvisedSupport 子类)
public List<Object> getInterceptorsAndDynamicInterceptionAdvice(Method method, @Nullable Class<?> targetClass) {
MethodCacheKey cacheKey = new MethodCacheKey(method);
//这里使用了缓存来提高效率,第一次获取还是要创建的。
//使用了 advisorChainFactory 生成拦截器链,advisorChainFactory 是 DefaultAdvisorChainFactory 的实例
List<Object> cached = this.methodCache.get(cacheKey);
if (cached == null) {
cached = this.advisorChainFactory.getInterceptorsAndDynamicInterceptionAdvice(
this, method, targetClass);
this.methodCache.put(cacheKey, cached);
}
return cached;
}
advisorChainFactory.getInterceptorsAndDynamicInterceptionAdvice()源码:
@Override
public List<Object> getInte