BeanFactoryPostProcessor 的处理逻辑
在Spring in Action - Spring Boot Application的启动过程中提到过更新ApplicationContext是Spring应用启动中的重要环节,而在更新ApplicationContext过程中invokeBeanFactoryPostProcessors是其中的重要一步。今天主要通过对源码的解读来了解该步骤的逻辑。
1. 源码解读
invokeBeanFactoryPostProcessors方法的实现逻辑主要是通过代理类org.springframework.context.support.PostProcessorRegistrationDelegate来实现的。其关键逻辑如下:
// Invoke BeanDefinitionRegistryPostProcessors first, if any.
if (beanFactory instanceof BeanDefinitionRegistry) {
// Do not initialize FactoryBeans here: We need to leave all regular beans
// uninitialized to let the bean factory post-processors apply to them!
// Separate between BeanDefinitionRegistryPostProcessors that implement
// PriorityOrdered, Ordered, and the rest.
// First, invoke the BeanDefinitionRegistryPostProcessors that implement PriorityOrdered.
// Next, invoke the BeanDefinitionRegistryPostProcessors that implement Ordered.
// Finally, invoke all other BeanDefinitionRegistryPostProcessors until no further ones appear.
// Now, invoke the postProcessBeanFactory callback of all processors handled so far.
} else {
// Invoke factory processors registered with the context instance.
}
// Do not initialize FactoryBeans here: We need to leave all regular beans
// uninitialized to let the bean factory post-processors apply to them!
// Separate between BeanFactoryPostProcessors that implement PriorityOrdered,
// Ordered, and the rest.
// First, invoke the BeanFactoryPostProcessors that implement PriorityOrdered.
// Next, invoke the BeanFactoryPostProcessors that implement Ordered.
// Finally, invoke all other BeanFactoryPostProcessors.
// Clear cached merged bean definitions since the post-processors might have
// modified the original metadata, e.g. replacing placeholders in values...
优先级顺序概括来讲就是:
1. BeanDefinitionRegistryPostProcessor优先级高于BeanFactoryPostProcessor;
2. 预置PostProcessor的优先级高于自定义的PostProcessor;
3. PriorityOrdered > Ordered > 其他;
2. 基于Spring boot 2.1.5-RELEASE源码跟踪
2.1 预置post processor
总共预置BeanFactoryPostProcessor三个,其中BeanDefinitionRegistryPostProcessor的实现2个,他们的postProcessBeanDefinitionRegistry方法会优先执行。
SharedMetadataReaderFactoryContextInitializer.CachingMetadataReaderFactoryPostProcessor
注册名称为org.springframework.boot.autoconfigure.internalCachingMetadataReaderFactory的bean definition;
设置名称为org.springframework.context.annotation.internalConfigurationAnnotationProcessor的bean的metadataReaderFactory属性为org.springframework.boot.autoconfigure.internalCachingMetadataReaderFactory。
ConfigurationWarningsApplicationContextInitializer.ConfigurationWarningsPostProcessor
检查ComponentScan package是否为空或包含org或org.springframework包。
2.2 执行BeanDefinitionRegistryPostProcessor
- 执行PriorityOrdered实现的postProcessBeanDefinitionRegistry方法
org.springframework.context.annotation.ConfigurationClassPostProcessor
@Configuration注解类处理器,基本上所有的bean definition都是通过这个类进行加载的。它会递归寻找所有的配置类并完成bean definition的加载工作。
- 执行Ordered实现的postProcessBeanDefinitionRegistry方法
- 执行其他的postProcessBeanDefinitionRegistry方法
CounterBeanFactoryPostProcessor1为自定义processor,用于统计当前bean definition的信息。
执行完这一步基本上所有的bean definition都已经加载完毕。
- 执行所有BeanDefinitionRegistryPostProcessor实现的postProcessBeanFactory方法
如上述自定义processor: CounterBeanFactoryPostProcessor1.postProcessBeanFactory
- 执行预置BeanFactoryPostProcessor实现的postProcessBeanFactory方法
3.2 执行BeanFactoryPostProcessor
- 执行PriorityOrdered实现的postProcessBeanFactory方法
- 执行Ordered实现的postProcessBeanFactory方法
- 执行其他processor的postProcessBeanFactory方法