ConfigurationClassPostProcessor
是BeanFactoryPostProcessor
的一个实现类,并且实现了PriorityOrdered
接口,如下图所示:
classDiagram
BeanFactoryPostProcessor <|-- BeanDefinitionRegistryPostProcessor
BeanDefinitionRegistryPostProcessor <|-- ConfigurationClassPostProcessor
PriorityOrdered <|-- ConfigurationClassPostProcessor
在ConfigurableApplicationContext
的refresh()
过程中,postProcessBeanDefinitionRegistry()
和postProcessBeanFactory()
方法被依次回调,触发processConfigBeanDefinitions()
的执行,流程如下图所示:
flowchart TD
a1["postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry)"]
a2["postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)"]
subgraph one["processConfigBeanDefinitions(registry)"]
b1["String[] candidateNames = registry.getBeanDefinitionNames();"]
b2["ConfigurationClassUtils.checkConfigurationClassCandidate(beanDef, this.metadataReaderFactory)"]
b3["configCandidates.sort((bd1, bd2))"]
b1-->b2-->b3
end
a1 --> one
a2 --> one
在processConfigBeanDefinitions()
方法中,主要有两个功能:
BeanDefinitionRegistry
找到符合ConfigurationClass
定义的BeanDefinition
并排序;ConfigurationClassParser
解析ConfigurationClass
。