核心子方法5: invokeBeanFactoryPostProcessors(beanFactory)方法详解
先总结: 该方法通过指定顺序, 遍历调用各种实现了BeanDefinitionRegistryPostProcessor接口或BeanFactoryPostProcessor接口, 的beanFactory后处理器
注: BeanDefinitionRegistryPostProcessor接口继承了BeanFactoryPostProcessor接口
调用顺序:
1.先调用已经提前放入ApplicationContext应用程序上下文beanFactoryPostProcessors属性中的实现了BeanDefinitionRegistryPostProcessor接口的postProcessBeanDefinitionRegistry方法
2.按照优先级调用实现了PriorityOrdered接口(有PriorityOrdered注解)的BeanDefinitionRegistryPostProcessor -> postProcessBeanDefinitionRegistry方法
3.按照优先级调用实现了Ordered接口(有Ordered注解)的BeanDefinitionRegistryPostProcessor -> postProcessBeanDefinitionRegistry方法
4.最后,调用所有剩下的BeanDefinitionRegistryPostProcessors -> postProcessBeanDefinitionRegistry方法
5.按照上面的顺序调用BeanDefinitionRegistryPostProcessors -> postProcessBeanFactory方法
6.调用已经提前放入ApplicationContext应用程序上下文中实现了BeanFactoryPostProcessor接口的postProcessBeanFactory方法
7.按照优先级调用实现了PriorityOrdered接口(有PriorityOrdered注解)的BeanFactoryPostProcessor-> postProcessBeanFactory方法
8.按照优先级调用实现了Ordered接口(有Ordered注解)的BeanFactoryPostProcessor-> postProcessBeanFactory方法
9.最后,调用所有剩下的BeanFactoryPostProcessor-> postProcessBeanFactory方法
调用总结:
1.优先调用实现BeanDefinitionRegistryPostProcessor接口的, 后调用实现BeanFactoryPostProcessor接口的
2. 优先提前放入ApplicationContext应用程序上下文的, 后调用实现了PriorityOrdered接口的, 再调用实现了Ordered接口的, 最后调用其它的
核心BeanFactory后处理器: ConfigurationClassPostProcessor
此类是一个后置处理器的类,主要功能是参与BeanFactory的建造,主要功能如下
1、解析加了@Configuration的配置类
2、解析@ComponentScan扫描的包
3、解析@ComponentScans扫描的包
4、解析@Componen注解
5、解析@Import注解
6、解析@ImportResource注解
7、解析@PropertySource注解
8、解析@Bean注解
方法1: postProcessBeanDefinitionRegistry 定位、加载、解析、注册相关注解
方法2: postProcessBeanFactory 对加了@Configuration的配置类进行CGLIB增强处理(代理配置类),添加ImportAwareBeanPostProcessor后置处理类