Spring 纯注解开发--011--BeanFactoryPostProcessor

一、基本概念
  • BeanPostProcessor:bean后置处理器,bean创建对象初始化后进行拦截工作的
  • BeanFactoryPostProcessor:beanFactory的后置处理器。
    • 在BeanFactory标准初始化之后调用,所有的bean定义已经保存加载到beanFactory中,但是bean的实例还未创建。可以用来定制和修改BeanFactory的内容
二、验证代码
  • 可以创建了一个对象Blue,验证是否是再BeanFactoryPostProcessor之后,进行初始化
@Component
public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        System.out.println("MyBeanFactoryPostProcessor....postProcessBeanFactory...");
        int beanDefinitionCount = beanFactory.getBeanDefinitionCount();
        String[] beanDefinitionNames = beanFactory.getBeanDefinitionNames();

        System.out.println("容器中的bean的个数"+beanDefinitionCount);
        Arrays.stream(beanDefinitionNames).forEach(str ->{
            System.out.println(str);
        });
    }
}
  • 结果解析
    • BeanFactoryPostProcessor日志打印,以及Blue的BeanDefinition的打印
      Spring 纯注解开发--011--BeanFactoryPostProcessor_第1张图片
    • 然后是Blue的构造方法打印出的信息 在这里插入图片描述
三、 源码
  1. IOC容器创建对象 Spring 纯注解开发--011--BeanFactoryPostProcessor_第2张图片
  2. invokeBeanFactoryPostProcessors(beanFactory);执行BeanFactoryPostProcessorsSpring 纯注解开发--011--BeanFactoryPostProcessor_第3张图片
    1. 如何找到所有的BeanFactoryPostProcessor,上面方法中的部分代码
      invokeBeanFactoryPostProcessors(beanFactory){
      	//1. 从bean工厂中,拿到所有BeanFactoryPostProcessor类型对象的名称
      	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. 对BeanFactoryPostProcessor进行分类,分到三个list中
      	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);
      		}
      	}
      
      
      	//3. 对三种不同的BeanFactoryPostProcessor进行分别处理
      	//首先获取,实现了PriorityOrdered接口的BeanFactoryPostProcessor
      	// First, invoke the BeanFactoryPostProcessors that implement PriorityOrdered.
      	sortPostProcessors(priorityOrderedPostProcessors, beanFactory);
      	invokeBeanFactoryPostProcessors(priorityOrderedPostProcessors, beanFactory);
      
      	//然后,再找实习了Ordered接口的BeanFactoryPostProcessor
      	// Next, invoke the BeanFactoryPostProcessors that implement Ordered.
      	List<BeanFactoryPostProcessor> orderedPostProcessors = new ArrayList<>();
      	for (String postProcessorName : orderedPostProcessorNames) {
      		orderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class));
      	}
      	sortPostProcessors(orderedPostProcessors, beanFactory);
      	invokeBeanFactoryPostProcessors(orderedPostProcessors, beanFactory);
      
      	//最后找,其他的BeanFactoryPostProcessor
      	// Finally, invoke all other BeanFactoryPostProcessors.
      	List<BeanFactoryPostProcessor> nonOrderedPostProcessors = new ArrayList<>();
      	for (String postProcessorName : nonOrderedPostProcessorNames) {
      		nonOrderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class));
      	}
      
      	//nonOrderedPostProcessors所有普通的BeanFactoryPostProcessor
      	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();
      }
      
    2. 直接在BeanFactory中找到所有类型是BeanFactoryPostProcessor的组件,并执行他们的方法。
      //执行的方法
      /**
       * Invoke the given BeanFactoryPostProcessor beans.
       */
      private static void invokeBeanFactoryPostProcessors(
      		Collection<? extends BeanFactoryPostProcessor> postProcessors, ConfigurableListableBeanFactory beanFactory) {
      	//执行BeanFactoryPostProcessor后置处理器中的处理方法
      	for (BeanFactoryPostProcessor postProcessor : postProcessors) {
      		//执行处理器方法
      		postProcessor.postProcessBeanFactory(beanFactory);
      	}
      }
      
  • BeanFactoryPostProcessors的执行时机是在初始化创建其他组件前面执行。

你可能感兴趣的:(------,1.,spring注解)