ReflectiveMethodInvocation
用来处理AOP拦截器链的调用;
<1>proceed方法
①方法返回值为Object
②
//判断拦截器是否已经处理到最后一个了如果是,则通过反射方法直接调用目标方法
if(this.currentInterceptorIndex == this.interceptorAndDynamicMethodMatchers.size() - 1)) {
return this.invokeJoinpoint();
}
③
//循环拦截器链,对每一个能够匹配到的拦截器进行调用处理
Object interceptorOrinterceptionAdvice = this.interceptorAndDynamicMethodMatchers.get(++this.currentInterceptorIndex);
/** 区分拦截器是否为InterceptorAndDynamicMethodMatcher类型,如果不为此类型则直接调用
* MethodInterceptor的invoke方法进行拦截处理;
* 如果是InterceptorAndDynamicMethodMatcher类型,则判断匹配与否,匹配则进行invoke调用,否则调用
**/ 下一个拦截器
if(interceptorOrInterceptionAdvice instanceof InterceptorAndDynamicMethodMatcher) {
InterceptorAndDynamicMethodMatcher dm = (InterceptorAndDynamicMethodMatcher)interceptorOrInterceptionAdvice;
return dm.MethodMatcher.matches(this.method, this.targetClass, this.arguments) ? dm.interceptor.invoke(this) : this.proceed();
} else {
((MethodInterceptor)interceptorAndInterceptionAdvice).invoke(this);
}
InterceptorAndDynamicMethodMatcher
拦截器和动态方法匹配器,最终的实现过程是MethodMatcher的实现类去做的
class InterceptorAndDynamicMethodMatcher {
//拦截器
final MethodInterceptor interceptor;
//拦截器的匹配的实际执行者
final MethodMatcher methodMatcher;
public InterceptorAndDynamicMethodMatcher(MethodInterceptor interceptor, MethodMatcher methodMatcher) {
this.interceptor = interceptor;
this.methodMatcher = methodMatcher;
}
}
JdkDynamicAopProxy
<1>invoke方法中获取拦截器链
//获取拦截器链
List<Object> chain = this.advised.getInterceptorsAndDynamicInterceptionAdvice(method, targetClass);
AdvisedSupport
AOP代理配置管理器的基类;
- <1>拦截器链的缓存容器
//transient不序列化
private transient Map<MethodCacheKey, List<Object>> methodCache;
- <2>获取拦截器链
public List<Object> getInterceptorsAndDynamicInterceptionAdvice(Method method, Class<?> targetClass) {
//构建拦截器链缓存容器的key
MethodCacheKey cacheKey = new MethodCacheKey(method);
//从缓存容器中获取拦截器链
List<Object> cached = this.methodCache.get(cacheKey);
//如果获取的拦截器链还是为空,则通过DefaultAdvisorChainFactory创建拦截器链
if(cached == null) {
cached = this.advisorChainFactory.getInterceptorsAndDynamicInterceptionAdvice(this, method, targetClass);
//缓存拦截器链
this.methodCache.put(cacheKey, cached);
}
return cached;
}
<3>拦截器链的实际创建者
AdvisorChainFactory advisorChainFactory = new DefaultAdvisorChainFactory();