spring IOC 源码阅读(三)之invokeBeanFactoryPostProcessors(beanFactory)

6 篇文章 0 订阅
6 篇文章 0 订阅

看完beanFactory的创建,接下来就是执行BeanFactoryPostProcessors的接口

  1. 执行BeanDefinitionRegistryPostProcessor类型的BeanFactoryPostProcessors
    1. 从beanFactory中获取到所有BeanDefinitionRegistryPostProcessor类型的beanName
    2. 首先获取到所有PriorityOrdered级别的BeanDefinitionRegistryPostProcessor,并通过beanFactory.getBean()方法创建bean对象
    3. 然后执行postProcessor.postProcessBeanDefinitionRegistry(registry)
    4. 然后获取Ordered级别的并创建bean对象,执行postProcessBeanDefinitionRegistry
    5. 最后创建其他的BeanDefinitionRegistryPostProcessor类型的bean对象,并执行他的postProcessBeanDefinitionRegistry方法
    6. 最后执行这些BeanDefinitionRegistryPostProcessor的postProcessBeanFactory方法
  2. 执行除了BeanDefinitionRegistryPostProcessor以外的其他的BeanFactoryPostProcessors
    1. 获取所有的BeanFactoryPostProcessor类型的beanName
    2. 将PriorityOrdered级别的创建bean对象后放入priorityOrderedPostProcessors集合中
    3. 将Ordered级别的beanName放入orderedPostProcessorNames集合中
    4. 将其他的beanName放入nonOrderedPostProcessorNames集合中
    5. 执行priorityOrderedPostProcessors集合中的BeanFactoryPostProcessor的postProcessBeanFactory方法
    6. 创建orderedPostProcessorNames集合中所有的beanName的BeanFactoryPostProcessor对象
    7. 执行orderedPostProcessorNames集合中创建出来的BeanFactoryPostProcessor的postProcessBeanFactory方法
    8. 创建nonOrderedPostProcessorNames集合中所有的beanName的BeanFactoryPostProcessor对象
    9. 执行nonOrderedPostProcessorNames集合中创建出来的BeanFactoryPostProcessor的postProcessBeanFactory方法
public static void invokeBeanFactoryPostProcessors(
      ConfigurableListableBeanFactory beanFactory, List<BeanFactoryPostProcessor> beanFactoryPostProcessors) {

   //用来存放已经执行过的BeanFactoryPostProcessor的beanName
   Set<String> processedBeans = new HashSet<>();

   if (beanFactory instanceof BeanDefinitionRegistry) {
      BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
      //用来保存beanFactoryPostProcessors中的非BeanDefinitionRegistryPostProcessor类型的BeanFactoryPostProcessor
      List<BeanFactoryPostProcessor> regularPostProcessors = new ArrayList<>();
      //用来保存所有的BeanDefinitionRegistryPostProcessor,方便最后执行BeanDefinitionRegistryPostProcessor的PostProcessorBeanFactory方法
      List<BeanDefinitionRegistryPostProcessor> registryProcessors = new ArrayList<>();
      
      for (BeanFactoryPostProcessor postProcessor : beanFactoryPostProcessors) {
         if (postProcessor instanceof BeanDefinitionRegistryPostProcessor) {
             //如果是BeanDefinitionRegistryPostProcessor类型的BeanFactoryPostProcessor
             //那么就执行它的postProcessBeanDefinitionRegistry方法,然后添加到registryProcessors集合中
            BeanDefinitionRegistryPostProcessor registryProcessor =
                  (BeanDefinitionRegistryPostProcessor) postProcessor;
            registryProcessor.postProcessBeanDefinitionRegistry(registry);
            registryProcessors.add(registryProcessor);
         }
         else {
             //如果不是BeanDefinitionRegistryPostProcessor类型的BeanFactoryPostProcessor
             //那么就添加到regularPostProcessors集合中
            regularPostProcessors.add(postProcessor);
         }
      }
      //用来保存当前需要执行的BeanDefinitionRegistryPostProcessor
      List<BeanDefinitionRegistryPostProcessor> currentRegistryProcessors = new ArrayList<>();

      // 执行PriorityOrdered级别的BeanDefinitionRegistryPostProcessor的PostProcessorsBeanDefinitionRegistry方法
      //获取到所有的BeanDefinitionRegistryPostProcessor类型的BeanFactoryPostProcessor的名称
      String[] postProcessorNames =
            beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
      for (String ppName : postProcessorNames) {
         if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
             //遍历所有的BeanDefinitionRegistryPostProcessor
             //找出所有的PriorityOrdered级别的,并通过beanFactory.getBean()创建对象
             //然后添加到currentRegistryProcessors集合中
             //同时将名称添加到processedBeans集合中
            currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
            processedBeans.add(ppName);
         }
      }
      sortPostProcessors(currentRegistryProcessors, beanFactory);
      //将PriorityOrdered级别的BeanDefinitionRegistryPostProcessor放入registryProcessors中
      registryProcessors.addAll(currentRegistryProcessors);
      //执行PriorityOrdered级别的BeanDefinitionRegistryPostProcessor的PostProcessorsBeanDefinitionRegistry方法
      invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
      //将currentRegistryProcessors清空
      currentRegistryProcessors.clear();

      //执行PriorityOrdered级别的BeanDefinitionRegistryPostProcessor的PostProcessorsBeanDefinitionRegistry方法
      //获取到所有的BeanDefinitionRegistryPostProcessor类型的BeanFactoryPostProcessor的名称
      postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
      for (String ppName : postProcessorNames) {
         if (!processedBeans.contains(ppName) && beanFactory.isTypeMatch(ppName, Ordered.class)) {
            //找出没有在上面步骤中执行过的所有的Ordered级别的BeanDefinitionRegistryPostProcessor
            //并通过beanFactory.getBean()创建对象
            //然后添加到currentRegistryProcessors集合中
            //同时将名称添加到processedBeans集合中
            currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
            processedBeans.add(ppName);
         }
      }
      sortPostProcessors(currentRegistryProcessors, beanFactory);
      //将Ordered级别的BeanDefinitionRegistryPostProcessor放入registryProcessors中
      registryProcessors.addAll(currentRegistryProcessors);
       //执行Ordered级别的BeanDefinitionRegistryPostProcessor的PostProcessorsBeanDefinitionRegistry方法
      invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
      currentRegistryProcessors.clear();

      //执行剩余的BeanDefinitionRegistryPostProcessor的PostProcessorsBeanDefinitionRegistry方法
      boolean reiterate = true;
      while (reiterate) {
         reiterate = false;
         //获取到所有的BeanDefinitionRegistryPostProcessor类型的BeanFactoryPostProcessor的名称
         postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
         for (String ppName : postProcessorNames) {
            if (!processedBeans.contains(ppName)) {
               //找出没有在上面步骤中执行过的所有BeanDefinitionRegistryPostProcessor
               //并通过beanFactory.getBean()创建对象
               //然后添加到currentRegistryProcessors集合中
               //同时将名称添加到processedBeans集合中
               currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
               processedBeans.add(ppName);
               reiterate = true;
            }
         }
         sortPostProcessors(currentRegistryProcessors, beanFactory);
         //将剩余的BeanDefinitionRegistryPostProcessor放入registryProcessors中
         registryProcessors.addAll(currentRegistryProcessors);
         //执行剩余的BeanDefinitionRegistryPostProcessor的PostProcessorsBeanDefinitionRegistry方法
         invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
         currentRegistryProcessors.clear();
      }

      //然后执行BeanDefinitionRegistryPostProcessor的postProcessorsBeanFactory方法
      invokeBeanFactoryPostProcessors(registryProcessors, beanFactory);
      //然后执行beanFactoryPostProcessors中非BeanDefinitionRegistryPostProcessor
      //的BeanFactoryPostProcessor的postProcessorsBeanFactory方法
      invokeBeanFactoryPostProcessors(regularPostProcessors, beanFactory);
   }

   else {
      // Invoke factory processors registered with the context instance.
      invokeBeanFactoryPostProcessors(beanFactoryPostProcessors, beanFactory);
   }

   // 获取所有的BeanFactoryPostProcessor类型的beanName
   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<>();
   for (String ppName : postProcessorNames) {
      if (processedBeans.contains(ppName)) {
         //如果是之前执行过的,就跳过
      }
      else if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
          //如果没有执行过,并且是PriorityOrdered级别的
          //则通过beanFactory.getBean创建bean对象,并加入到priorityOrderedPostProcessors集合中
         priorityOrderedPostProcessors.add(beanFactory.getBean(ppName, BeanFactoryPostProcessor.class));
      }
      else if (beanFactory.isTypeMatch(ppName, Ordered.class)) {
          //如果是Ordered级别的,则将名称加入到orderedPostProcessorNames集合中
         orderedPostProcessorNames.add(ppName);
      }
      else {
          //剩下没执行过并且既不是PriorityOrdered级别又不是Ordered级别BeanFactoryPostProcessor的名称加入到nonOrderedPostProcessorNames集合中
         nonOrderedPostProcessorNames.add(ppName);
      }
   }

   //先执行PriorityOrdered级别的BeanFactoryPostProcessor的PostProcessorsBeanFactory方法
   sortPostProcessors(priorityOrderedPostProcessors, beanFactory);
   invokeBeanFactoryPostProcessors(priorityOrderedPostProcessors, beanFactory);

   // 再执行Ordered级别的BeanFactoryPostProcessor的PostProcessorsBeanFactory方法
   List<BeanFactoryPostProcessor> orderedPostProcessors = new ArrayList<>(orderedPostProcessorNames.size());
   for (String postProcessorName : orderedPostProcessorNames) {
       //创建Ordered级别的BeanFactoryPostProcessor的对象,并加入到orderedPostProcessors集合中
      orderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class));
   }
   sortPostProcessors(orderedPostProcessors, beanFactory);
   //执行Ordered级别的BeanFactoryPostProcessor的PostProcessorsBeanFactory方法
   invokeBeanFactoryPostProcessors(orderedPostProcessors, beanFactory);

   // 最后执行剩余的BeanFactoryPostProcessor的PostProcessorsBeanFactory方法
   List<BeanFactoryPostProcessor> nonOrderedPostProcessors = new ArrayList<>(nonOrderedPostProcessorNames.size());
   for (String postProcessorName : nonOrderedPostProcessorNames) {
       //创建剩余的BeanFactoryPostProcessor的对象,并加入到nonOrderedPostProcessors集合中
      nonOrderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class));
   }
   //执行剩余的BeanFactoryPostProcessor的PostProcessorsBeanFactory方法
   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();
   }

spring IOC 源码阅读(四)之registerBeanPostProcessors(beanFactory)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值