spring中aop源码分析(五)

spring中源码分析(五)

我们接着JdkDynamicAopProxy下面的invoke方法下面 进行分析


   // We need to create a method invocation...
   invocation = new ReflectiveMethodInvocation(proxy, target, method, args, targetClass, chain);
   // Proceed to the joinpoint through the interceptor chain.
   retVal = invocation.proceed();

上面核心的为两句一个是为了构造对象一个是执行上面方法:

《spring中aop源码分析(五)》

这里发现它实现了ProxyMethodInvocation接口

我们进入到构造方法中,里面只是简单的进行赋值操作

protected ReflectiveMethodInvocation(
      Object proxy, @Nullable Object target, Method method, @Nullable Object[] arguments,
      @Nullable Class<?> targetClass, List<Object> interceptorsAndDynamicMethodMatchers) {

   this.proxy = proxy;
   this.target = target;
   this.targetClass = targetClass;
   this.method = BridgeMethodResolver.findBridgedMethod(method);
   this.arguments = AopProxyUtils.adaptArgumentsIfNecessary(method, arguments);
   this.interceptorsAndDynamicMethodMatchers = interceptorsAndDynamicMethodMatchers;
}

如何我们看它的procced方法

public Object proceed() throws Throwable {
   // 如果当前拦截器索引为最后一个直接执行目标方法
   if (this.currentInterceptorIndex == this.interceptorsAndDynamicMethodMatchers.size() - 1) {
      return invokeJoinpoint();
   }

  //获取指定索引上面的拦截器并将索引加一
   Object interceptorOrInterceptionAdvice =
         this.interceptorsAndDynamicMethodMatchers.get(++this.currentInterceptorIndex);
  //这里查看拦截器是否为动态拦截器一般都不是这种拦截器
   if (interceptorOrInterceptionAdvice instanceof InterceptorAndDynamicMethodMatcher) {
      InterceptorAndDynamicMethodMatcher dm =
            (InterceptorAndDynamicMethodMatcher) interceptorOrInterceptionAdvice;
      if (dm.methodMatcher.matches(this.method, this.targetClass, this.arguments)) {
         return dm.interceptor.invoke(this);
      }
      else {
         // Dynamic matching failed.
         // Skip this interceptor and invoke the next in the chain.
         return proceed();
      }
   } else {
      //这里就调用拦截器上面的方法 这里拦截器可能为ThrowsAdviceInterceptor
      return ((MethodInterceptor) interceptorOrInterceptionAdvice).invoke(this);
   }
}
    原文作者:Spring Boot
    原文地址: https://blog.csdn.net/yinbucheng/article/details/81088496
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞