SpringAOP拦截器调用

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();

 

 

 

 

 

 

 

 

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值