springBoot容器刷新
上篇说了启动过程,现在说其中的刷新。上代码
public void refresh() throws BeansException, IllegalStateException {
synchronized (this.startupShutdownMonitor) {
//刷新前准备
prepareRefresh();
//获取工厂
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
//工厂预处理(给工厂设置一些必要参数 类加载器 后置处理等)
prepareBeanFactory(beanFactory);
try {
//工厂后置处理
postProcessBeanFactory(beanFactory);
//执行beanFactory后置处理器
invokeBeanFactoryPostProcessors(beanFactory);
//注册bean后置处理器
registerBeanPostProcessors(beanFactory);
//初始化信息(国际化等)
initMessageSource();
//初始化应用事件派发器
initApplicationEventMulticaster();
//子类实现(web容器就是在这里加载的)
onRefresh();
//注册监听器
registerListeners();
// 实例化剩下的bean(单例)
finishBeanFactoryInitialization(beanFactory);
// 后续操作(初始化生命周期等)
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 'acti