Spring源码 11 IOC refresh方法6

Spring IOC 的核心是 AbstractApplicationContextrefresh 方法。
其中一共有 13 个主要方法,这里分析第 6 个:registerBeanPostProcessors

1 AbstractApplicationContext

1-1 注册 Bean 增强器

registerBeanPostProcessors(beanFactory)
protected void registerBeanPostProcessors(ConfigurableListableBeanFactory beanFactory) {
    // 注册 Bean 增强器
    PostProcessorRegistrationDelegate.registerBeanPostProcessors(beanFactory, this);
}

1-2 注册 Bean 增强器

PostProcessorRegistrationDelegate.registerBeanPostProcessors(beanFactory, this)

2 PostProcessorRegistrationDelegate

public static void registerBeanPostProcessors(ConfigurableListableBeanFactory beanFactory, AbstractApplicationContext applicationContext) {
   String[] postProcessorNames = beanFactory.getBeanNamesForType(BeanPostProcessor.class, true, false);
   /**
    * BeanPostProcessorChecker 是一个普通的信息打印,可能会有些情况
    * 当 Spring 的配置中的后处理器还没有被注册就已经开始了 bean 的初始化时
    * 便会打印出 BeanPostProcessorChecker 中设定的信息
    */
   int beanProcessorTargetCount = beanFactory.getBeanPostProcessorCount() + 1 + postProcessorNames.length;
   beanFactory.addBeanPostProcessor(new BeanPostProcessorChecker(beanFactory, beanProcessorTargetCount));
   // 使用 PriorityOrdered 保证顺序
   List<BeanPostProcessor> priorityOrderedPostProcessors = new ArrayList<>();
   List<BeanPostProcessor> internalPostProcessors = new ArrayList<>();
   // 使用 Ordered 保证顺序
   List<String> orderedPostProcessorNames = new ArrayList<>();
   // 无序 BeanPostProcessor
   List<String> nonOrderedPostProcessorNames = new ArrayList<>();
   for (String ppName : postProcessorNames) {
      if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
         BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
         priorityOrderedPostProcessors.add(pp);
         if (pp instanceof MergedBeanDefinitionPostProcessor) {
            internalPostProcessors.add(pp);
         }
      } else if (beanFactory.isTypeMatch(ppName, Ordered.class)) {
         orderedPostProcessorNames.add(ppName);
      } else {
         nonOrderedPostProcessorNames.add(ppName);
      }
   }
   // 第 1 步,排序增强器。注册所有实现 PriorityOrdered 的 BeanPostProcessor
   sortPostProcessors(priorityOrderedPostProcessors, beanFactory);
   // 注册 Bean 增强器
   registerBeanPostProcessors(beanFactory, priorityOrderedPostProcessors);
   // 第 2 步,注册所有实现 Ordered 的 BeanPostProcessor
   List<BeanPostProcessor> orderedPostProcessors = new ArrayList<>(orderedPostProcessorNames.size());
   for (String ppName : orderedPostProcessorNames) {
      BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
      orderedPostProcessors.add(pp);
      if (pp instanceof MergedBeanDefinitionPostProcessor) {
         internalPostProcessors.add(pp);
      }
   }
   sortPostProcessors(orderedPostProcessors, beanFactory);
   registerBeanPostProcessors(beanFactory, orderedPostProcessors);
   // 第 3 步,注册所有无序的 BeanPostProcessor
   List<BeanPostProcessor> nonOrderedPostProcessors = new ArrayList<>(nonOrderedPostProcessorNames.size());
   for (String ppName : nonOrderedPostProcessorNames) {
      BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
      nonOrderedPostProcessors.add(pp);
      if (pp instanceof MergedBeanDefinitionPostProcessor) {
         internalPostProcessors.add(pp);
      }
   }
   registerBeanPostProcessors(beanFactory, nonOrderedPostProcessors);
   /**
    * 第 4 步,注册所有 MergedBeanDefinitionPostProcessor 类型的 BeanPostProcessor,并非重复注册
    * 在 beanFactory.addBeanPostProcessor 中会先移除已经存在的 BeanPostProcessor
    */
   sortPostProcessors(internalPostProcessors, beanFactory);
   registerBeanPostProcessors(beanFactory, internalPostProcessors);
   // 添加 ApplicationListener 探测器
   beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(applicationContext));
}

2-1 排序增强器

sortPostProcessors(priorityOrderedPostProcessors, beanFactory)
private static void sortPostProcessors(List<?> postProcessors, ConfigurableListableBeanFactory beanFactory) {
   if (postProcessors.size() <= 1) {
      return;
   }
   Comparator<Object> comparatorToUse = null;
   if (beanFactory instanceof DefaultListableBeanFactory) {
      comparatorToUse = ((DefaultListableBeanFactory) beanFactory).getDependencyComparator();
   }
   if (comparatorToUse == null) {
      comparatorToUse = OrderComparator.INSTANCE;
   }
   postProcessors.sort(comparatorToUse);
}

2-2 注册 Bean 增强器

registerBeanPostProcessors(beanFactory, priorityOrderedPostProcessors)
private static void registerBeanPostProcessors(
      ConfigurableListableBeanFactory beanFactory, List<BeanPostProcessor> postProcessors) {

   if (beanFactory instanceof AbstractBeanFactory) {
      // Bulk addition is more efficient against our CopyOnWriteArrayList there
      ((AbstractBeanFactory) beanFactory).addBeanPostProcessors(postProcessors);
   }
   else {
      for (BeanPostProcessor postProcessor : postProcessors) {
         beanFactory.addBeanPostProcessor(postProcessor);
      }
   }
}

参考

https://www.bilibili.com/video/BV1tR4y1F75R?spm_id_from=333.337.search-card.all.click

https://www.bilibili.com/video/BV12Z4y197MU?spm_id_from=333.999.0.0

《Spring源码深度解析(第2版)》

版本

Spring 5.3.15

posted @ 2022-03-14 13:58  天航星  阅读(34)  评论(0编辑  收藏  举报