9.refresh-invokeBeanFactoryPostProcessor方法脉络整理

【接上文】

前面用三篇介绍了对一系列注解的解析过程,下面我们再回到最初的那个方法,看下整体脉络。

public static void invokeBeanFactoryPostProcessors(
      ConfigurableListableBeanFactory beanFactory, List<BeanFactoryPostProcessor> beanFactoryPostProcessors) {

   // Invoke BeanDefinitionRegistryPostProcessors first, if any.
   Set<String> processedBeans = new HashSet<>();
   // 通过上面的代码可以知道beanFactory的实现类其实是DefaultListableBeanFactory,我们暂且只看下这个类的定义。
   // public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFactory
   //	implements ConfigurableListableBeanFactory, BeanDefinitionRegistry, Serializable
   // 可以看到这里的DefaultListableBeanFactory是实现了BeanDefinitionRegistry接口的,这里的if判断结果是true
   if (beanFactory instanceof BeanDefinitionRegistry) {
      BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
      List<BeanFactoryPostProcessor> regularPostProcessors = new ArrayList<>();
      List<BeanDefinitionRegistryPostProcessor> registryProcessors = new ArrayList<>();
      //这里的beanFactoryPostProcessor 会不会包含自定义的beanFactoryPostProcessor呢
      //大概猜测下 这里之前的步骤并没有检测beanFactoryPostProcessor的过程,这里如果有的话就是系统内置的beanFactoryPostProcessor
      //在AbstractApplicationContext中传参使用的是默认的值 也就是新new出来的一个list 这里的for循环目前是不会走里面的内容的。
      for (BeanFactoryPostProcessor postProcessor : beanFactoryPostProcessors) {
         if (postProcessor instanceof BeanDefinitionRegistryPostProcessor) {
            BeanDefinitionRegistryPostProcessor registryProcessor =
                  (BeanDefinitionRegistryPostProcessor) postProcessor;
            registryProcessor.postProcessBeanDefinitionRegistry(registry);
            registryProcessors.add(registryProcessor);
         }
         else {
            regularPostProcessors.add(postProcessor);
         }
      }

      // Do not initialize FactoryBeans here: We need to leave all regular beans
      // uninitialized to let the bean factory post-processors apply to them!
      // Separate between BeanDefinitionRegistryPostProcessors that implement
      // PriorityOrdered, Ordered, and the rest.
      List<BeanDefinitionRegistryPostProcessor> currentRegistryProcessors = new ArrayList<>();

      // First, invoke the BeanDefinitionRegistryPostProcessors that implement PriorityOrdered.
      //CONFIGURATION_ANNOTATION_PROCESSOR_BEAN_NAME --> ConfigurationClassPostProcessor -->实现了BeanDefinitionRegistryPostProcessor,PriorityOrdered
      String[] postProcessorNames =
            beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
      //所以这里取到的beanName是CONFIGURATION_ANNOTATION_PROCESSOR_BEAN_NAME这个常量对应的值
      for (String ppName : postProcessorNames) {
         //判断这个bean是否实现了PriorityOrdered接口 显然这里是返回true的
         //NOTE:前面的分析中提到过可以用户自定义特殊beanFactory的操作 所以这里的循环和if判断都是有价值的
         if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
		    //beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class)获取到的就是ConfigurationClassPostProcessor
            currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
            //通过beanFactory.getBean的方法调用,这里的bean已经在容器中真实存在了, 也就是ConfigurationClassPostProcessor
            processedBeans.add(ppName);
         }
      }
      //对后置处理器进行排序 
      sortPostProcessors(currentRegistryProcessors, beanFactory);
      //加入到已注册的后置处理器集合中
      registryProcessors.addAll(currentRegistryProcessors);
      //这里真正调用BeanDefinitionRegistryPostProcessors后置处理器的方法。 
      //这里currentRegistryProcessors里面装的就是ConfigurationClassPostProcessor
      invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry, beanFactory.getApplicationStartup());
      currentRegistryProcessors.clear();

      // Next, invoke the BeanDefinitionRegistryPostProcessors that implement Ordered.
      //上面invokeBeanDefinitionRegistryPostProcessors这个方法的内容已经详细分析过了,可以知道一些特定的bean已经被解析并加入到容器中了。
      //这里再通过BeanDefinitionRegistryPostProcessor这个类型去获取bean的时候已经可以获取到自定义的BeanDefinitionRegistryPostProcessor的实现了。
      postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
      for (String ppName : postProcessorNames) {
         if (!processedBeans.contains(ppName) && beanFactory.isTypeMatch(ppName, Ordered.class)) {
            currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
            processedBeans.add(ppName);
         }
      }
      sortPostProcessors(currentRegistryProcessors, beanFactory);
      registryProcessors.addAll(currentRegistryProcessors);
      //这里又调用了一次这个方法
      invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry, beanFactory.getApplicationStartup());
      currentRegistryProcessors.clear();

      // Finally, invoke all other BeanDefinitionRegistryPostProcessors until no further ones appear.
      //这里通过一个while循环迭代所有的BeanDefinitionRegistryPostProcessor,生成bean并放入容器中
      boolean reiterate = true;
      while (reiterate) {
         reiterate = false;
         postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
         for (String ppName : postProcessorNames) {
             //与上面代码的区别是这里没有了beanFactory.isTypeMatch(ppName, Ordered.class)这个判断
            // 结合上面两套类似的逻辑,可以发现这里其实把BeanDefinitionRegistryPostProcessor分成了三类,
            // 就是1.实现了PriorityOrdered  2.实现了Ordered接口 3. 其他接口 按顺序依次处理1 2 3 的情况
            if (!processedBeans.contains(ppName)) {
               currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
               processedBeans.add(ppName);
               reiterate = true;
            }
         }
         sortPostProcessors(currentRegistryProcessors, beanFactory);
         registryProcessors.addAll(currentRegistryProcessors);
         invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry, beanFactory.getApplicationStartup());
         currentRegistryProcessors.clear();
      }
      // 处理BeanFactoryPostProcessor的回调
      // Now, invoke the postProcessBeanFactory callback of all processors handled so far.
      // 因为BeanDefinitionRegistryPostProcessor是BeanFactoryPostProcessor的子接口,
      // 这里的处理是先回调实现了BeanDefinitionRegistryPostProcessor接口的postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)方法,
      invokeBeanFactoryPostProcessors(registryProcessors, beanFactory);
      // 再调用直接实现了BeanFactoryPostProcessor接口的postProcessBeanFactory方法。
      invokeBeanFactoryPostProcessors(regularPostProcessors, beanFactory);
   } else {
      // Invoke factory processors registered with the context instance.
      invokeBeanFactoryPostProcessors(beanFactoryPostProcessors, beanFactory);
   }

   // Do not initialize FactoryBeans here: We need to leave all regular beans
   // uninitialized to let the bean factory post-processors apply to them!
   String[] postProcessorNames =
         beanFactory.getBeanNamesForType(BeanFactoryPostProcessor.class, true, false);

   // Separate between BeanFactoryPostProcessors that implement PriorityOrdered,
   // Ordered, and the rest.
   List<BeanFactoryPostProcessor> priorityOrderedPostProcessors = new ArrayList<>();
   List<String> orderedPostProcessorNames = new ArrayList<>();
   List<String> nonOrderedPostProcessorNames = new ArrayList<>();
   // 处理上面步骤中没有处理过的接口实现
   //  2. Ordered
   for (String ppName : postProcessorNames) {
      if (processedBeans.contains(ppName)) {
         // skip - already processed in first phase above
         //如果上面处理过了,直接跳过。
      }
      //1. 实现了PriorityOrdered接口的 这里直接处理了成bean了,后面就处理了另外两种情况
      else if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
         priorityOrderedPostProcessors.add(beanFactory.getBean(ppName, BeanFactoryPostProcessor.class));
      }
      //2. 实现了Ordered接口的
      else if (beanFactory.isTypeMatch(ppName, Ordered.class)) {
         orderedPostProcessorNames.add(ppName);
      }
      //3. 其他情况
      else {
         nonOrderedPostProcessorNames.add(ppName);
      }
   }

   // First, invoke the BeanFactoryPostProcessors that implement PriorityOrdered.
   sortPostProcessors(priorityOrderedPostProcessors, beanFactory);
   // 排序后调用他们的回调方法
   invokeBeanFactoryPostProcessors(priorityOrderedPostProcessors, beanFactory);

   // Next, invoke the BeanFactoryPostProcessors that implement Ordered.
   List<BeanFactoryPostProcessor> orderedPostProcessors = new ArrayList<>(orderedPostProcessorNames.size());
   for (String postProcessorName : orderedPostProcessorNames) {
      //生成对应的bean后加入到对应的list中
      orderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class));
   }
   sortPostProcessors(orderedPostProcessors, beanFactory);
   //调用回调方法
   invokeBeanFactoryPostProcessors(orderedPostProcessors, beanFactory);

   // Finally, invoke all other BeanFactoryPostProcessors.
   //生成其他情况的bean 并调用回调方法
   List<BeanFactoryPostProcessor> nonOrderedPostProcessors = new ArrayList<>(nonOrderedPostProcessorNames.size());
   for (String postProcessorName : nonOrderedPostProcessorNames) {
      nonOrderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class));
   }
   invokeBeanFactoryPostProcessors(nonOrderedPostProcessors, beanFactory);

   // Clear cached merged bean definitions since the post-processors might have
   // modified the original metadata, e.g. replacing placeholders in values...
   beanFactory.clearMetadataCache();
}

//invokeBeanFactoryPostProcessors
/**
 * Invoke the given BeanFactoryPostProcessor beans.
 * 跟想象中的一样,遍历调用接口回调方法
 */
private static void invokeBeanFactoryPostProcessors(
        Collection<? extends BeanFactoryPostProcessor> postProcessors, ConfigurableListableBeanFactory beanFactory) {

    for (BeanFactoryPostProcessor postProcessor : postProcessors) {
        StartupStep postProcessBeanFactory = beanFactory.getApplicationStartup().start("spring.context.bean-factory.post-process")
                .tag("postProcessor", postProcessor::toString);
        postProcessor.postProcessBeanFactory(beanFactory);
        postProcessBeanFactory.end();
    }
}

到这里其实所有的BeanDefinitionRegistryPostProcessor和BeanFactoryPostProcessor都已经成为容器中的bean了。

【小结】

根据文中注释大概了解了整个流程脉络:

  1. 通过类型BeanDefinitionRegistryPostProcessor获取到内置的实现ConfigurationClassPostProcessor
  2. 调用ConfigurationClassPostProcessor的postProcessBeanDefinitionRegistry回调方法解析了
    @Component @Import @ImportSource @Bean 这一系列注解的bean
  3. 再次根据类型BeanDefinitionRegistryPostProcessor获取到beanFactory中的所有实现
  4. 排序 然后依次执行postProcessBeanDefinitionRegistry方法
  5. 解析实现了BeanFactoryPostProcessor接口的方法
  6. 排序 然后依次执行postProcessBeanFactory方法

【NEXT】

refresh-registerBeanPostProcessors方法解析

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

泽济天下

你的鼓励是我最大的动力。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值