Spring常见扩展点-BeanFactoryPostProcessor

171 阅读2分钟

最近在看一些微服务的源码(Feign),这些框架中都是基于Spring的扩展点做自己的功能实现,为了方便理解源码,在此对一些扩展点做一下记录,方便更好的理解其加载时机。

PS:下方标注的执行顺序纯属个人理解,括号内为加载的相对顺序。 数字越小,优先级最高

BeanFactoryPostProcessor (51)

见名知意,Bean工厂的后置处理器。

主要就是在Spring完成Bean加载后,如果想要扩展一部分逻辑,可以实现该接口。

该处理器,执行顺序并不高。先看这个单纯只是因为,很多优先级高的处理器依赖他。

定义

image.png

这里注意一下这个参数: ConfigurableListableBeanFactory 这个参数是 Spring 上下文中很重要的一个接口欧:Spring IOC 容器实例就是在这个接口的默认实现类中 DefaultListableBeanFactory

所以这个接口能够获取所有交给Spring托管的类

image.png

方法调用时机

  • Spring start
  • AbstractApplicationContext#refresh()
    • invokeBeanFactoryPostProcessors()
  • PostProcessorRegistrationDelegate#invokeBeanFactoryPostProcessors()

image.png

关于invokeBeanFactoryPostProcessors 方法的执行,这里有一些需要注意的点:

  1. 参数会携带 Spring 自己定义的BeanFactoryPostProcessor 实现类,这部分会优先执行。
  2. BeanFactoryPostProcessor 还存在一个子接口:BeanDefinitionRegistryPostProcessor 实现该接口的类会优先执行其方法 postProcessBeanDefinitionRegistry , postProcessBeanFactory 方法后执行。
  3. BeanFactoryPostProcessor实现类的执行也是有顺序的,基于其实现的排序接口以及值大小: PriorityOrdered > ordered > 其他。
public static void invokeBeanFactoryPostProcessors(
      ConfigurableListableBeanFactory beanFactory, List<BeanFactoryPostProcessor> beanFactoryPostProcessors) {

   Set<String> processedBeans = new HashSet<>();

   // 一: 这里 beanFactory 类型很重要 通常也都是这里
   if (beanFactory instanceof BeanDefinitionRegistry) {
      BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
      // 这里做了一下拆分 
      List<BeanFactoryPostProcessor> regularPostProcessors = new ArrayList<>();
      List<BeanDefinitionRegistryPostProcessor> registryProcessors = new ArrayList<>();

      for (BeanFactoryPostProcessor postProcessor : beanFactoryPostProcessors) {
          // 拆分子接口
         if (postProcessor instanceof BeanDefinitionRegistryPostProcessor) {
            BeanDefinitionRegistryPostProcessor registryProcessor =
                  (BeanDefinitionRegistryPostProcessor) postProcessor;
            // 先调用子接口 扩展的方法 postProcessBeanDefinitionRegistry
            registryProcessor.postProcessBeanDefinitionRegistry(registry);
            // 再加入结合
            registryProcessors.add(registryProcessor);
         }
         else {
            regularPostProcessors.add(postProcessor);
         }
      }

     
      List<BeanDefinitionRegistryPostProcessor> currentRegistryProcessors = new ArrayList<>();
      // 扫描所有 BeanDefinitionRegistryPostProcessor 的实现类,加入列表,先执行实现方法
      String[] postProcessorNames =
            beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
      for (String ppName : postProcessorNames) {
         if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
            currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
            processedBeans.add(ppName);
         }
      }
      sortPostProcessors(currentRegistryProcessors, beanFactory);
      registryProcessors.addAll(currentRegistryProcessors);
      // 先执行 BeanDefinitionRegistryPostProcessor 实现类
      invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry, beanFactory.getApplicationStartup());
      currentRegistryProcessors.clear();
      // 继续扫描 继续添加 继续执行
      // Next, invoke the BeanDefinitionRegistryPostProcessors that implement Ordered.
      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.
      boolean reiterate = true;
      while (reiterate) {
         reiterate = false;
         postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
         for (String ppName : postProcessorNames) {
            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();
      }

      // Now, invoke the postProcessBeanFactory callback of all processors handled so far.
      // 最后再执行 postProcessBeanFactory 方法...
      invokeBeanFactoryPostProcessors(registryProcessors, beanFactory);
      invokeBeanFactoryPostProcessors(regularPostProcessors, beanFactory);
   }

   else {  
      invokeBeanFactoryPostProcessors(beanFactoryPostProcessors, beanFactory);
   }

   // 扫描 BeanFactoryPostProcessor 实现类
   String[] postProcessorNames =
         beanFactory.getBeanNamesForType(BeanFactoryPostProcessor.class, true, false);

   // 按顺序 分类执行
   List<BeanFactoryPostProcessor> priorityOrderedPostProcessors = new ArrayList<>();
   List<String> orderedPostProcessorNames = new ArrayList<>();
   List<String> nonOrderedPostProcessorNames = new ArrayList<>();
   for (String ppName : postProcessorNames) {
      if (processedBeans.contains(ppName)) {
         // skip - already processed in first phase above
      }
      else if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
         priorityOrderedPostProcessors.add(beanFactory.getBean(ppName, BeanFactoryPostProcessor.class));
      }
      else if (beanFactory.isTypeMatch(ppName, Ordered.class)) {
         orderedPostProcessorNames.add(ppName);
      }
      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) {
      orderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class));
   }
   sortPostProcessors(orderedPostProcessors, beanFactory);
   invokeBeanFactoryPostProcessors(orderedPostProcessors, beanFactory);

   // Finally, invoke all other BeanFactoryPostProcessors.
   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();
}

BeanDefinitionRegistryPostProcessor (50)

上边也提到了这个扩展点。作为 BeanFactoryPostProcessor 的子接口,其执行顺序要高于 BeanFactoryPostProcessor 。执行时机、位置都与 BeanFactoryPostProcessor一致,因此这里就不多做介绍。

定义

image.png

方法调用时机

调用时机与 BeanFactoryPostProcessor 一致,这里不做描述。

备注

该接口有个很重要的实现类,需要说明一下: ConfigurationClassPostProcessor

提到这个类是因为,他会调用 ImportBeanDefinitionRegistrar 接口的实现类, 这个接口的实现类我们就比较熟悉了,可以看一下:

image.png