spring中aop源码解析(二)

 2019-10-17 21:33  阅读(1289)
文章分类:Spring boot

Spring中aop源码解析(二)

我们上一章讲到判断对象是否需要被代理

if (isInfrastructureClass(bean.getClass()) || shouldSkip(bean.getClass(), beanName)) {
       this.advisedBeans.put(cacheKey, Boolean.FALSE);
       return bean;
    }

上面有两个方法我们需要进入源码再进行深入分析下,一个是isInfrastructureClass 和shouldSkip

protected boolean isInfrastructureClass(Class<?> beanClass) {
       //这里调用父类的返回 或者如果类上面有@Aspect注解 就认为是基础建设类返回true
       return (super.isInfrastructureClass(beanClass) ||
             (this.aspectJAdvisorFactory != null && this.aspectJAdvisorFactory.isAspect(beanClass)));
    }

上面的方法是AnnotationAwareAspectJAutoProxyCreator上的方法它的服务类AbstractAutoProxyCreator

protected boolean isInfrastructureClass(Class<?> beanClass) {
      //这里判断传入的类是不是Advice/Pointcut/Advisor/AopInfrastructureBean的类或子类
       boolean retVal = Advice.class.isAssignableFrom(beanClass) ||
             Pointcut.class.isAssignableFrom(beanClass) ||
             Advisor.class.isAssignableFrom(beanClass) ||
             AopInfrastructureBean.class.isAssignableFrom(beanClass);
       return retVal;
    }

下面我们进入另一个重要的方法:

protected boolean shouldSkip(Class<?> beanClass, String beanName) {
       // 获取所有候选的Advisor
       List<Advisor> candidateAdvisors = findCandidateAdvisors();
       for (Advisor advisor : candidateAdvisors) {
         //如果beanName为AspectJPointAdvisor直接返回true
          if (advisor instanceof AspectJPointcutAdvisor &&
                ((AspectJPointcutAdvisor) advisor).getAspectName().equals(beanName)) {
             return true;
          }
       }
       return super.shouldSkip(beanClass, beanName);
    }

下面我们进入到AbstractAspectJAutoProxyCreator方法:

protected boolean shouldSkip(Class<?> beanClass, String beanName) {
       return false;
    }

很简单返回false不进行跳过


来源:[]()

点赞(0)
版权归原创作者所有,任何形式转载请联系作者; Java 技术驿站 >> spring中aop源码解析(二)

相关推荐