• Spring AOP 基于注解源码整理


    导入配置类

    1. EnableAspectJAutoProxy 注解导入 AspectJAutoProxyRegistrar
    2. ImportBeanDefinitionRegistrar#registerBeanDefinitions向容器中加入AnnotationAwareAspectJAutoProxyCreator
    3. AnnotationAwareAspectJAutoProxyCreator#initBeanFactory初始化ReflectiveAspectJAdvisorFactory
      protected void initBeanFactory(ConfigurableListableBeanFactory beanFactory) {
          super.initBeanFactory(beanFactory);
          if (this.aspectJAdvisorFactory == null) {
              this.aspectJAdvisorFactory = new ReflectiveAspectJAdvisorFactory(beanFactory);
          }
          this.aspectJAdvisorsBuilder =
                  new BeanFactoryAspectJAdvisorsBuilderAdapter(beanFactory, this.aspectJAdvisorFactory);
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8

    解析出所有的Advisor对象

    1. AbstractAutoProxyCreator#postProcessBeforeInstantiation
       public Object postProcessBeforeInstantiation(Class beanClass, String beanName) {
           Object cacheKey = getCacheKey(beanClass, beanName);
           if (!StringUtils.hasLength(beanName) || !this.targetSourcedBeans.contains(beanName)) {
               if (this.advisedBeans.containsKey(cacheKey)) {
                   return null;
               }
               if (isInfrastructureClass(beanClass) || shouldSkip(beanClass, beanName)) {
                   this.advisedBeans.put(cacheKey, Boolean.FALSE);
                   return null;
               }
           }
           return null ;
       }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
    2. AspectJAwareAdvisorAutoProxyCreator#shouldSkip
    3. AnnotationAwareAspectJAutoProxyCreator#findCandidateAdvisors
    4. BeanFactoryAspectJAdvisorsBuilder#buildAspectJAdvisors
    5. 获取Spring容器中被Aspect注解的bean, 通过ReflectiveAspectJAdvisorFactory#getAdvisors获取Advisor
      MetadataAwareAspectInstanceFactory factory =
          new BeanFactoryAspectInstanceFactory(this.beanFactory, beanName);
      List classAdvisors = this.advisorFactory.getAdvisors(factory);
      
      • 1
      • 2
      • 3
    6. ReflectiveAspectJAdvisorFactory#getAdvisors(factory)
      public List getAdvisors(MetadataAwareAspectInstanceFactory aspectInstanceFactory) {
          Class aspectClass = aspectInstanceFactory.getAspectMetadata().getAspectClass();
          String aspectName = aspectInstanceFactory.getAspectMetadata().getAspectName();
          MetadataAwareAspectInstanceFactory lazySingletonAspectInstanceFactory =
                   new LazySingletonAspectInstanceFactoryDecorator(aspectInstanceFactory);
          List advisors = new ArrayList<>();
          for (Method method : getAdvisorMethods(aspectClass)) {
               Advisor advisor = getAdvisor(method, lazySingletonAspectInstanceFactory, 0, aspectName);
               if (advisor != null) {
                   advisors.add(advisor);
               }
          }
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13

    生产代理对象

    1. AbstractAutoProxyCreator#postProcessAfterInitialization
       public Object postProcessAfterInitialization(@Nullable Object bean, String beanName) {
           Object cacheKey = getCacheKey(bean.getClass(), beanName);
           if (this.earlyProxyReferences.remove(cacheKey) != bean) {
               return wrapIfNecessary(bean, beanName, cacheKey);
           }
           return bean;
       }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
    2. AbstractAutoProxyCreator#wrapIfNecessary
      protected Object wrapIfNecessary(Object bean, String beanName, Object cacheKey) {
           // Create proxy if we have advice.
           Object[] specificInterceptors = getAdvicesAndAdvisorsForBean(bean.getClass(), beanName, null);
           this.advisedBeans.put(cacheKey, Boolean.TRUE);
           Object proxy = createProxy(
               bean.getClass(), beanName, specificInterceptors, new SingletonTargetSource(bean));
           this.proxyTypes.put(cacheKey, proxy.getClass());
           return proxy;
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
    获取符合条件的Advisor集合
    1. AbstractAdvisorAutoProxyCreator#getAdvicesAndAdvisorsForBean
      protected Object[] getAdvicesAndAdvisorsForBean(
               Class beanClass, String beanName, @Nullable TargetSource targetSource) {
          List advisors = findEligibleAdvisors(beanClass, beanName);
          if (advisors.isEmpty()) {
              return DO_NOT_PROXY;
          }
          return advisors.toArray();
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
    2. AbstractAdvisorAutoProxyCreator#findEligibleAdvisors
      protected List findEligibleAdvisors(Class beanClass, String beanName) {
           List candidateAdvisors = findCandidateAdvisors();
           List eligibleAdvisors = findAdvisorsThatCanApply(candidateAdvisors, beanClass, beanName);
           extendAdvisors(eligibleAdvisors);
           if (!eligibleAdvisors.isEmpty()) {
               eligibleAdvisors = sortAdvisors(eligibleAdvisors);
           }
           return eligibleAdvisors;
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
    3. AbstractAdvisorAutoProxyCreator#findAdvisorsThatCanApply
      protected List findAdvisorsThatCanApply(
               List candidateAdvisors, Class beanClass, String beanName) {
          ProxyCreationContext.setCurrentProxiedBeanName(beanName);
          try {
              return AopUtils.findAdvisorsThatCanApply(candidateAdvisors, beanClass);
          }finally {
              ProxyCreationContext.setCurrentProxiedBeanName(null);
          }
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
    创建代理对象
    1. AbstractAdvisorAutoProxyCreator#createProxy
  • 相关阅读:
    NDK环境搭建,以Windows平台为例
    python常用标准库(os系统模块、shutil文件操作模块)
    ZPL II 语言编程基础
    SprintBoot案例-增删改查
    vue watch监听
    源码解析springbatch的job是如何运行的?
    以太网协议介绍(ARP、UDP、ICMP、IP)
    C# 将音频PCM数据封装成wav文件
    Java基础:Java抽象接口
    1.2、Python基础-函数
  • 原文地址:https://blog.csdn.net/yichengjie_c/article/details/133549439