ApplicationContext的refresh过程
2021-06-17 17:01:29 0 举报
描述ApplicationContext的refresh过程
作者其他创作
大纲/内容
onRefresh
添加addBeanPostProcessor(new ImportAwareBeanPostProcessor
refreshBeanFactory
getEnvironment().validateRequiredProperties()
通过ClassPathBeanDefinitionScanner对配置类进行解析,扫描指定路径下指定注解的Bean,并注册Bean
如果有自定义BeanFactoryPostProcessor,且类型为BeanDefinitionRegistryPostProcessor,回首先得到执行
prepareBeanFactory设置ClassLoader、设置两个BeanPostProcessor(ApplicationContextAwareProcessor、ApplicationListenerDetector)设置依赖注入时需要忽略的接口(主要为aware接口)注册特殊注入的接口类型(BeanFactory、ResourceLoader、ApplicationEventPublisher、ApplicationContext)容器中放入Environment、SystemProperties、SystemEnvironment、ApplicationStartup四个实例
clearResourceCaches
spring.context.config-classes.parse解析配置类
postProcessBeanDefinitionRegistry
ConfigurationClassPostProcessor.postProcessBeanFactory
PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors
查找BeanFactory中的所有BeanDefinitionRegistryPostProcessor类型的Bean定义并按照PriorityOrdered->Ordered->其他的顺序依次执行当前ConfigurationClassPostProcessorpost.ProcessBeanDefinitionRegistry
finishBeanFactoryInitialization完成Bean定义中剩余为实例化的类的实例化工作,并调用SmartInitializingSingleton接口的实例的afterSingletonsInstantiated方法,这里是EventListenerMethodProcessor
spring.context.beans.post-process BeanFactory初始化完成后的处理机制
finishRefresh
initApplicationEventMulticaster
执行LifecycleProcessor的onRefresh方法
spring.context.bean-factory.post-process
spring.context.config-classes.enhance
这三步完成了BeanFactory的初始化
spring.context.beandef-registry.post-process Bean定义注册完成后处理机制
getBeanFactory()
publishEvent:ContextRefreshedEvent;
EventListenerMethodProcessor注册所有EventListener注解的方法为单独的ApplicationListener实例
prepareRefresh
org.springframework.context.support.AbstractApplicationContext#invokeBeanFactoryPostProcessors->ConfigurationClassPostProcessor->invokeBeanFactoryPostProcessors->PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors
在Bean初始化(目前为止未初始化的)前后调用
ConfigurationClassBeanDefinitionReader处理配置类中Bean注解的类的注册处理import的配置的Bean的注册
initPropertySources
初始化MessageSourceinitMessageSource
注册BeanPostProcessor-》org.springframework.context.support.AbstractApplicationContext#registerBeanPostProcessorsorg.springframework.context.annotation.internalAutowiredAnnotationProcessororg.springframework.context.annotation.internalCommonAnnotationProcessorBeanPostProcessorCheckerApplicationListenerDetector自定义BeanPostProcessor
自定义BeanFactoryPostProcessor执行
initLifecycleProcessor,该生命周期钩子需要Context创建后显式调用,如果是SmartLifecycle的实现则Spring会自动调用
obtainFreshBeanFactory
registerListeners
找到具有Configuration注解的Bean定义并按照@Order注解定义的顺序排序
收藏
收藏
0 条评论
回复 删除
下一页