==============abstractApplicationContext.refresh========================
prepareRefresh
obtainFreshBeanFactory(创建BeanFactory)
prepareBeanFactory(准备BeanFactory)
postProcessBeanFactory(子类实现处理beanFactory)
invokeBeanFactoryPostProcessors(执行beanfactory的后置处理器)
registerBeanPostProcessors(添加Bean后置处理器,可能会出现bean被误伤即提前被初始化的不到其他bean后置处理器的处理)
initMessageSource(初始化消息源)
initApplicationEventMulticaster(初始化事件多路广播器)
onRefresh(子类实现)
registerListeners(注册applicationListener监听器)
finishBeanFactoryInitialization(完成剩下的全部非延迟的单例Bean初始化)
finishRefresh(调用lifecycleProcessor的onRefresh和发布刷新事件)
resetCommonCaches(finally清空全部的元数据缓存)
@Override
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();
}
}
}