IOC容器初始化流程
- 一、概要
- 1.准备上下文prepareRefresh()
- 2. 获取beanFactory:obtainFreshBeanFactory()
- 3. 准备beanFactory:prepareBeanFactory(beanFactory)
- 4. 后置处理:postProcessBeanFactory()
- 5. 调用bean工厂后置处理器:invokeBeanFactoryPostProcessors()
- 6. 注册bean后置处理器:registerBeanPostProcessors()
- 7. 处理国际化:initMessageSource()
- 8. 注册事件多播器:initApplicationEventMulticaster()
- 9. 初始化特殊bean:onRefresh()
- 10. 注册监听器bean:registerListeners()
- 11.实例化剩下的所有非懒加载的单例bean:finishBeanFactoryInitialization
- 12. 发布结束刷新事件:finishRefresh()
- 二、小结
一、概要
了解过spring的都知道,spring是通过一个IOC容器管理应用中的bean对象的。本文讨论springboot应用在初始化IOC容器时的主要操作都有哪些。我在SpringBoot启动流程的2.6. 刷新容器(创建Bean实例)
中提到refreshContext(context)实际会调用到AbstractApplicationContext的refresh()方法。
java">public void refresh() throws BeansException, IllegalStateException {// ...省略部分代码...// 准备上下文,将上下文状态设置为active,设置启动时间// 初始化属性源、校验必填属性是否可解析等prepareRefresh();// Tell the subclass to refresh the internal bean factory.// 获取beanFactory,默认为DefaultListableBeanFactoryConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();// Prepare the bean factory for use in this context.// 准备上下文中要使用的beanFactory,为其设置一些属性值prepareBeanFactory(beanFactory);// Allows post-processing of the bean factory in context subclasses.// 由子类对beanFactory做后置处理postProcessBeanFactory(beanFactory);StartupStep beanPostProcess = this.applicationStartup.start("spring.context.beans.post-process");// Invoke factory processors registered as beans in the context.// 调用上下文中已经注册的bean工厂后置处理器invokeBeanFactoryPostProcessors(beanFactory);// Register bean processors that intercept bean creation.// 注册bean后置处理器registerBeanPostProcessors(beanFactory);beanPostProcess.end();// Initialize message source for this context.// 处理国际化initMessageSource();// Initialize event multicaster for this context.// 注册事件多播器initApplicationEventMulticaster();// Initialize other special beans in specific context subclasses.// 初始化特殊bean,如tomcatonRefresh();// Check for listener beans and register them.// 检查监听器bean并注册registerListeners();// Instantiate all remaining (non-lazy-init) singletons.// 实例化剩下的所有非懒加载的单例beanfinishBeanFactoryInitialization(beanFactory);// Last step: publish corresponding event.// 最后发布相应的结束刷新事件finishRefresh();//...省略部分代码...
}
1.准备上下文prepareRefresh()
为刷新上下文做一些准备工作,修改上下文的closed
/active
标记状态,校验必填属性,重置监听器状态等。
java">/*** Prepare this context for refreshing, setting its startup date and* active flag as well as performing any initialization of property sources.*/
protected void prepareRefresh() {// Switch to active.// 设置启动时间this.startupDate = System.currentTimeMillis();// 将关闭标记置为falsethis.closed.set(false);// 将活动标记置为truethis.active.set(true);// ... 省略部分代码 ...// Initialize any placeholder property sources in the context environment.// 占位用,默认没有任何处理,由子类覆盖进行一些属性源的初始化initPropertySources();// Validate that all properties marked as required are resolvable:// see ConfigurablePropertyResolver#setRequiredProperties// 检查所有标记为必填的属性是否都可解析getEnvironment().validateRequiredProperties();// Store pre-refresh ApplicationListeners...if