SpringIOC初始化流程
在spring初始化时就会注册7个后置处理器分别如下:
AbstractApplicationContext构造器中会初始化AnnotatedBeanDefinitionReader AnnotatedBeanDefinitionReader构造器中初始化以下后置处理器
以下6个后置处理器除了ConfigurationClassPostProcessor 是实现了BeanFactoryPostProccessor 及BeanDefinationRegistryPostProcessor 其余全是普通的 BeanPostProccessor
ConfigurationClassPostProcessor
-
这个类是解析添加了
@Configuration注解的类,并且实现了BeanFactoryPostProccessor及BeanDefinationRegistryPostProcessor在springioc初始化过程有有至关重要的作用 -
AutowiredAnnotationBeanPostProcessor -
RequiredAnnotationBeanPostProcessor -
CommonAnnotationBeanPostProcessor -
PersistenceAnnotationBeanPostProcessor -
EventListenerMethodProcessor -
DefaultEventListenerFactory
一切的一切都在下边这个方法中定义了整个初始化过程
AbstractApplicationContext.refresh()
public void refresh() throws BeansException, IllegalStateException {
synchronized (this.startupShutdownMonitor) {
// Prepare this context for refreshing.
prepareRefresh();
// Tell the subclass to refresh the internal bean factory.
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
// Prepare the bean factory for use in this context.
prepareBeanFactory(beanFactory);
try {
// Allows post-processing of the bean factory in context subclasses.
postProcessBeanFactory(beanFactory);
// Invoke factory processors registered as beans in the context.
invokeBeanFactoryPostProcessors(beanFactory);
// Register bean processors that intercept bean creation.
registerBeanPostProcessors(beanFactory);
// Initialize message source for this context.
initMessageSource();
// Initialize event multicaster for this context.
initApplicationEventMulticaster();
// Initialize other special beans in specific context subclasses.
onRefresh();
// Check for listener beans and register them.
registerListeners();
// Instantiate all remaining (non-lazy-init) singletons.
finishBeanFactoryInitialization(beanFactory);
// Last step: publish corresponding event.
finishRefresh();
}
catch (BeansException ex) {
if (logger.isWarnEnabled()) {
logger.warn("Exception encountered during context initialization - " +
"cancelling refresh attempt: " + ex);
}
// Destroy already created singletons to avoid dangling resources.
destroyBeans();
// Reset 'active' flag.
cancelRefresh(ex);
// Propagate exception to caller.
throw ex;
}
finally {
// Reset common introspection caches in Spring's core, since we
// might not ever need metadata for singleton beans anymore...
resetCommonCaches();
}
}
}
-
prepareRefresh()预刷新动作,主要是对一列成员属性的初始化动作
-
prepareBeanFactory(beanFactory)
ClassPathXmlApplictionContext在此方法完成xml解析及相关组件注册到Spring容器中 -
postProcessBeanFactory(beanFactory);空方法以便后续扩展
-
invokeBeanFactoryPostProcessors(beanFactory);
完成BeanDefinitionRegistryPostProcessor以及BeanFactoryPostProcessor接口调用
接口调用顺序
首先调用BeanDefinitionRegistryPostProcessor并进行排序排序规则如下- 实现
PrirotyOrder接口 - 实现了
Order接口 - 普通的接口
nonOrdered调用
再次调用BeanFactoryPostProcessor接口调用,规则同上
- 实现
-
registerBeanPostProcessors(beanFactory);
完成注册BeanPostProcessor注册及实例化 -
initMessageSource()
初始化国际化相关东西需重点关注 -
initApplicationEventMulticaster()
初始化事件广播器,首先获取beanName为applicationEventMulticaster如果容器中没有
默认使用SimpleApplicationEventMulticaster -
onRefresh()
空方法给以便后续扩展
SpringBoot 内嵌Tomcat在此方法完成方法调用 -
registerListeners()
注册Spring时间监听器实现了次ApplicationListener的所有类 -
finishBeanFactoryInitialization()
完成普通Bean的实例化 -
finishRefresh(beanFactory)
IOC容器刷新完成并发布ContextRefreshedEvent容器刷新完成事件
本文详细解析了Spring框架中IOC容器的初始化流程,包括7个关键后置处理器的注册过程,以及AbstractApplicationContext.refresh()方法中定义的整个初始化过程。重点介绍了ConfigurationClassPostProcessor在解析@Configuration注解类中的作用。
4269

被折叠的 条评论
为什么被折叠?



