java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > Spring invokeBeanFactoryPostProcessors

Spring invokeBeanFactoryPostProcessors方法刨析源码

作者:融极

invokeBeanFactoryPostProcessors该方法会实例化所有BeanDefinitionRegistryPostProcessor和BeanFactoryPostProcessor的实例并且执行postProcessBeanFactory与postProcessBeanDefinitionRegistry方法

概述

invokeBeanFactoryPostProcessor方法是spring核心方法之一,主要用来调用beanFactory后置处理器来修改beanDefinition。

该方法实例化并调用已经注册到beanFactory的beanFactoryPostProcessor实例。

invokeBeanFactoryPostProcessors

	/**
	 * 实例化并且调用所有已经注册了的beanFactoryPostProcessor,遵循指明的顺序
	 *
	 * Instantiate and invoke all registered BeanFactoryPostProcessor beans,
	 * respecting explicit order if given.
	 * <p>Must be called before singleton instantiation.
	 */
	protected void invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory) {
		// 获取到当前应用程序上下文的beanFactoryPostProcessors变量的值,并且实例化调用执行所有已经注册的beanFactoryPostProcessor
		// 默认情况下,通过getBeanFactoryPostProcessors()来获取已经注册的BFPP,但是默认是空的 
		PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(beanFactory, getBeanFactoryPostProcessors());
		// Detect a LoadTimeWeaver and prepare for weaving, if found in the meantime
		// (e.g. through an @Bean method registered by ConfigurationClassPostProcessor)
		if (beanFactory.getTempClassLoader() == null && beanFactory.containsBean(LOAD_TIME_WEAVER_BEAN_NAME)) {
			beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory));
			beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader()));
		}
	}

getBeanFactoryPostProcessors() 方法

方法获取自定义的BFPP方法,默认为空。

// TODO 如何设置自定义的BFPP?

invokeBeanFactoryPostProcessors方法

public static void invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory,

List beanFactoryPostProcessors){}

参数:ConfigurableListableBeanFactory beanFactory是refresh()方法中的obtainFreshBeanFactory()方法获取的。

是DefaultListableBeanFactory 类型,DefaultListableBeanFactory类实现了BeanDefinitionRegistry接口。

	public final ConfigurableListableBeanFactory getBeanFactory() {
		DefaultListableBeanFactory beanFactory = this.beanFactory;
		if (beanFactory == null) {
			throw new IllegalStateException("BeanFactory not initialized or already closed - " +
					"call 'refresh' before accessing beans via the ApplicationContext");
		}
		return beanFactory;
	}

所以会走下面分支

if (beanFactory instanceof BeanDefinitionRegistry) {
...
}

否则直接调用外部beanFactoryPostProcessors的postProcessBeanFactory方法。

invokeBeanFactoryPostProcessors(beanFactoryPostProcessors, beanFactory);

BeanFactoryPostProcessor和BeanDefinitionRegistryPostProcessor分类

  // 类型转换
  BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
  // 此处希望大家做一个区分,两个接口是不同的,BeanDefinitionRegistryPostProcessor是BeanFactoryPostProcessor的子集
  // BeanFactoryPostProcessor主要针对的操作对象是BeanFactory,而BeanDefinitionRegistryPostProcessor主要针对的操作对象是BeanDefinition
  // 存放BeanFactoryPostProcessor的集合
  List<BeanFactoryPostProcessor> regularPostProcessors = new ArrayList<>();
  // 存放BeanDefinitionRegistryPostProcessor的集合
  List<BeanDefinitionRegistryPostProcessor> registryProcessors = new ArrayList<>();
  // 首先处理入参中的beanFactoryPostProcessors,遍历所有的beanFactoryPostProcessors,将BeanDefinitionRegistryPostProcessor
  // 和BeanFactoryPostProcessor区分开
  for (BeanFactoryPostProcessor postProcessor : beanFactoryPostProcessors) {
      // 如果是BeanDefinitionRegistryPostProcessor
      if (postProcessor instanceof BeanDefinitionRegistryPostProcessor) {
          BeanDefinitionRegistryPostProcessor registryProcessor =
                  (BeanDefinitionRegistryPostProcessor) postProcessor;
          // 直接执行BeanDefinitionRegistryPostProcessor接口中的postProcessBeanDefinitionRegistry方法
          registryProcessor.postProcessBeanDefinitionRegistry(registry);
          // 添加到registryProcessors,用于后续执行postProcessBeanFactory方法
          registryProcessors.add(registryProcessor);
      } else {
          // 否则,只是普通的BeanFactoryPostProcessor,添加到regularPostProcessors,用于后续执行postProcessBeanFactory方法
          regularPostProcessors.add(postProcessor);
      }
  }

处理实现了PriorityOrdered接口的BeanDefinitionRegistryPostProcessor类

  List<BeanDefinitionRegistryPostProcessor> currentRegistryProcessors = new ArrayList<>();
  // First, invoke the BeanDefinitionRegistryPostProcessors that implement PriorityOrdered.
  // 调用所有实现PriorityOrdered接口的BeanDefinitionRegistryPostProcessor实现类
  // 找到所有实现BeanDefinitionRegistryPostProcessor接口bean的beanName
  String[] postProcessorNames =
          beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
  // 遍历处理所有符合规则的postProcessorNames
  for (String ppName : postProcessorNames) {
      // 检测是否实现了PriorityOrdered接口
      if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
          // 获取名字对应的bean实例,添加到currentRegistryProcessors中
          currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
          // 将要被执行的BFPP名称添加到processedBeans,避免后续重复执行
          processedBeans.add(ppName);
      }
  }
  // 按照优先级进行排序操作
  sortPostProcessors(currentRegistryProcessors, beanFactory);
  // 添加到registryProcessors中,用于最后执行postProcessBeanFactory方法
  registryProcessors.addAll(currentRegistryProcessors);
  // 遍历currentRegistryProcessors,执行postProcessBeanDefinitionRegistry方法
  invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
  // 执行完毕之后,清空currentRegistryProcessors
  currentRegistryProcessors.clear();

处理实现了Ordered的BeanDefinitionRegistryPostProcessor类

 postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
 for (String ppName : postProcessorNames) {
     // 检测是否实现了Ordered接口,并且还未执行过
     if (!processedBeans.contains(ppName) && beanFactory.isTypeMatch(ppName, Ordered.class)) {
         // 获取名字对应的bean实例,添加到currentRegistryProcessors中
         currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
         // 将要被执行的BFPP名称添加到processedBeans,避免后续重复执行
         processedBeans.add(ppName);
     }
 }
 // 按照优先级进行排序操作
 sortPostProcessors(currentRegistryProcessors, beanFactory);
 // 添加到registryProcessors中,用于最后执行postProcessBeanFactory方法
 registryProcessors.addAll(currentRegistryProcessors);
 // 遍历currentRegistryProcessors,执行postProcessBeanDefinitionRegistry方法
 invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
 // 执行完毕之后,清空currentRegistryProcessors
 currentRegistryProcessors.clear();

处理剩下的BeanDefinitionRegistryPostProcessor类

  // 最后,调用所有剩下的BeanDefinitionRegistryPostProcessors
  boolean reiterate = true;
  while (reiterate) {
      reiterate = false;
      // 找出所有实现BeanDefinitionRegistryPostProcessor接口的类
      postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
      // 遍历执行
      for (String ppName : postProcessorNames) {
          // 跳过已经执行过的BeanDefinitionRegistryPostProcessor
          if (!processedBeans.contains(ppName)) {
              // 获取名字对应的bean实例,添加到currentRegistryProcessors中
              currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
              // 将要被执行的BFPP名称添加到processedBeans,避免后续重复执行
              processedBeans.add(ppName);
              reiterate = true;
          }
      }
      // 按照优先级进行排序操作
      sortPostProcessors(currentRegistryProcessors, beanFactory);
      // 添加到registryProcessors中,用于最后执行postProcessBeanFactory方法
      registryProcessors.addAll(currentRegistryProcessors);
      // 遍历currentRegistryProcessors,执行postProcessBeanDefinitionRegistry方法
      invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
      // 执行完毕之后,清空currentRegistryProcessors
      currentRegistryProcessors.clear();
  }

注意这里的reiterate变量在每找到一个未执行的BeanDefinitionRegistryPostProcessor实例都会被设置为true,表示invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);方法执行时可能会生成新的BeanDefinitionRegistryPostProcessor实例。

调用所有BeanDefinitionRegistryPostProcessor的postProcessBeanFactory方法

  // 调用所有BeanDefinitionRegistryPostProcessor的postProcessBeanFactory方法
  invokeBeanFactoryPostProcessors(registryProcessors, beanFactory);
  // 最后,调用入参beanFactoryPostProcessors中的普通BeanFactoryPostProcessor的postProcessBeanFactory方法
  invokeBeanFactoryPostProcessors(regularPostProcessors, beanFactory);

处理BeanFactory容器中注册的BeanFactoryPostProcessor类

处理方式与上面处理BeanDefinitionRegistryPostProcessor类似,按照先后顺序分别处理实现了PriorityOrdered接口、Ordered接口、没有实现Ordered接口的bean。这里不在详细说明。

   // 到这里为止,入参beanFactoryPostProcessors和容器中的所有BeanDefinitionRegistryPostProcessor已经全部处理完毕,下面开始处理容器中
   // 所有的BeanFactoryPostProcessor
   // 可能会包含一些实现类,只实现了BeanFactoryPostProcessor,并没有实现BeanDefinitionRegistryPostProcessor接口,
   // 因为有processedBeans记录了上面处理的实现了BeanDefinitionRegistryPostProcessor的类,所以不会重复处理。
   String[] postProcessorNames = beanFactory.getBeanNamesForType(BeanFactoryPostProcessor.class, true, false);
   regularPostProcessors = new ArrayList();
   registryProcessors = new ArrayList();
   currentRegistryProcessors = new ArrayList();
   postProcessorNames = postProcessorNames;
   int var20 = postProcessorNames.length;
   String ppName;
   for(var9 = 0; var9 < var20; ++var9) {
       ppName = postProcessorNames[var9];
       if (!processedBeans.contains(ppName)) {
           if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
               regularPostProcessors.add(beanFactory.getBean(ppName, BeanFactoryPostProcessor.class));
           } else if (beanFactory.isTypeMatch(ppName, Ordered.class)) {
               registryProcessors.add(ppName);
           } else {
               currentRegistryProcessors.add(ppName);
           }
       }
   }
   sortPostProcessors(regularPostProcessors, beanFactory);
   invokeBeanFactoryPostProcessors((Collection)regularPostProcessors, (ConfigurableListableBeanFactory)beanFactory);
   List<BeanFactoryPostProcessor> orderedPostProcessors = new ArrayList();
   Iterator var21 = registryProcessors.iterator();
   while(var21.hasNext()) {
       String postProcessorName = (String)var21.next();
       orderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class));
   }
   sortPostProcessors(orderedPostProcessors, beanFactory);
   invokeBeanFactoryPostProcessors((Collection)orderedPostProcessors, (ConfigurableListableBeanFactory)beanFactory);
   List<BeanFactoryPostProcessor> nonOrderedPostProcessors = new ArrayList();
   Iterator var24 = currentRegistryProcessors.iterator();
   while(var24.hasNext()) {
       ppName = (String)var24.next();
       nonOrderedPostProcessors.add(beanFactory.getBean(ppName, BeanFactoryPostProcessor.class));
   }
   invokeBeanFactoryPostProcessors((Collection)nonOrderedPostProcessors, (ConfigurableListableBeanFactory)beanFactory);
   // 因为后置处理器可能已经修改了原始元数据,例如,替换值中的占位符
   beanFactory.clearMetadataCache();

到此这篇关于Spring invokeBeanFactoryPostProcessors方法刨析源码的文章就介绍到这了,更多相关Spring invokeBeanFactoryPostProcessors内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文