Spring scan bean summary

purpose

From the principle of scanning beans to @Configuration source code
, several blogs about spring source code are all parsed around a method

invokeBeanFactoryPostProcessors(beanFactory);
org.springframework.context.support.AbstractApplicationContext#invokeBeanFactoryPostProcessors

So, I intend to make a small summary of this method in this blog

  1. When entering this method, the implementation class of the BeanFactoryPostProcessor injected by the programmer through the api (ac.addBeanFactoryPostProcessor();) will be executed (note here: if a class implements the BeanFactoryPostProcessor or implements the BeanDefinitionRegistryPostProcessor interface, we can all call it (It is the implementation class of BeanFactoryPostProcessor)
  2. The implementation class corresponding to the sub-interface BeanDefinitionRegistryPostProcessor of the BeanFactoryPostProcessor injected in the first step above will be executed first. At this time, the business logic code implemented by the programmer is executed, but this kind of scenario should be less, because at this time, even Beans have not been scanned into beanDefinitionMap, but since spring provides response extension points, it is always a useful scenario
  3. If the programmer does not provide the implementation class through the api, the implementation class of the BeanDefinitionRegistryPostProcessor will be obtained from the beanDefinitionMap. Theoretically speaking, at this time, only one is obtained, which is ConfigurationClassPostProcessor. The reason is not explained here. It was mentioned in the previous blog.
  4. Will execute first. org.springframework.context.annotation.ConfigurationClassPostProcessor#postProcessBeanDefinitionRegistryHere, the scanning of the bean is completed (@ComponentScan annotation, @Bean, @Import annotation). After this method is executed, the business class provided by the programmer will be scanned into the beanDefinitionMap
  5. After the business bean is scanned into the beanDefinitionMap, the implementation class of BeanDefinitionRegistryPostProcessor will be obtained from beanDefinitionMap, and will be executed in accordance with the following priority. PriorityOrdered> Ordered> Universal BeanDefinitionRegistryPostProcessor implementation class; why do you want to do this here? Because it is possible that the programmer himself also provides the implementation class of BeanDefinitionRegistryPostProcessor or BeanFactoryPostProcessor; therefore, it is necessary to scan and execute the business logic provided by the programmer
  6. After executing the postProcessBeanDefinitionRegistry method of BeanDefinitionRegistryPostProcessor, then execute the postProcessBeanFactory method of BeanFactoryPostProcessor
  7. ①, first execute spring's built-in, annotation injection, API injection, the corresponding postProcessorBeanFactory method in the BeanDefinitionRegistryPostProcessor implementation class (because an implementation class implements the method of BeanDefinitionRegistryPostProcessor, the method in the BeanFactoryPostProcessor interface must be implemented)
    ②, and then Execute the postProcessorBeanFactory method corresponding to the BeanFactoryPostProcessor implementation class injected by the programmer through the API
    ③, the implementation class of the BeanFactoryPostProcessor injected by the executive programmer through the annotation method; execute in the following order of priority: the implementation class that implements the PriorityOrdered interface is executed first> implemented The implementation class of the Ordered interface is executed first> the ordinary BeanFactoryPostProcessor implementation class is executed last

In the above process, in section 7.①, here, when the postProcessBeanFactory method of ConfigurationClassPostProcessor is executed, a proxy object will be generated for the full configuration class (@Configuration added)

So, this step in the spring in the source code, is to org.springframework.beans.factory.config.BeanFactoryPostProcessor#postProcessBeanFactory 和 org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor#postProcessBeanDefinitionRegistrydeal with these two classes implement the interface, but at the time of treatment, there will be time order, interface class has extension classes to implement the order, and programmers Spring's built-in implementation classes have a sequential execution order, so in general, you must first clarify the execution timing and order, and then deduct the details of each method step by step

Therefore, for the execution order of BeanFactoryPostProcesosr and BeanDefinitionRegistryPostProcessor, I have also sorted out:

1.首先执行BeanDefinitionRegistryPostProcessor的实现类
	1.1 首先执行程序员通过API的方式注入的BeanDefinitionRegistryPostProcessor实现类
	1.2 再执行spring内置的ConfigurationClassPostProcessor对应的postProcessorBeanDefinitionRegistry方法
	1.3 执行程序员通过@Component注解方式注入的BeanDefinitionRegistryPostProcessor的实现类
		1.3.1 实现了PriorityOrdered接口的实现类先执行
		1.3.2 其次是实现了Order接口的
		1.3.3 最后会通过一个while循环,处理所有普通的BeanDefinitionRegistryPostProcessor的实现类,直到beanDefinitionMap中不存在未处理的BeanDefinitionRegistryPostProcessor的实现类


2.其次执行BeanFactoryPostProcessor的实现类
	2.1 先执行spring内置的、通过注解注入的、通过API注入的,BeanDefinitionRegistryPostProcessor实现类中对应的postProcessorBeanFactory方法(因为一个实现类,实现BeanDefinitionRegistryPostProcessor的方法时,必然要实现BeanFactoryPostProcessor接口中的方法);在这里,spring把beanDefinitionRegistryPostProcessor的实现类中的postProcessorBeanFactory()放在了一起来执行
	2.2 再执行程序员通过API的方式注入的BeanFactoryPostProcessor实现类对应的postProcessorBeanFactory方法(需要注意这里和2.1的区别,举个例子:如果我自己声明了A类,实现了BeanDefinitionRegistryPostProcessor实现类;声明了B类,实现了BeanFactoryPostProcessor接口;那么:A类和B类中的postProcessorBeanFactory()方法都会被执行,只是A类中的方法是在2.1执行的,B类中的是在2.2或者2.3这里)
	2.3 执行程序员通过注解方式注入的BeanFactoryPostProcessor的实现类
		2.3.1 实现了PriorityOrdered接口的实现类先执行
		2.3.2 实现了Ordered接口的实现类先执行
		2.3.3 普通的BeanFactoryPostProcessor实现类最后执行

Guess you like

Origin blog.csdn.net/CPLASF_/article/details/107183200