@Override
public void refresh() throws BeansException, IllegalStateException {
synchronized (this.startupShutdownMonitor) {
// Prepare this context for refreshing.
prepareRefresh();
// 這裡是在子類中啟動refreshBeanFactory()的地方
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
// Prepare the bean factory for use in this context.
prepareBeanFactory(beanFactory);
try {
//設定BeanFactory的後置處理
postProcessBeanFactory(beanFactory);
//調用BeanFactory的後處理器,這些後處理器是在Bean定義中向容器注冊的
invokeBeanFactoryPostProcessors(beanFactory);
// Register bean processors that intercept bean creation.
registerBeanPostProcessors(beanFactory);
//對上下文中的消息源進行初始化
initMessageSource();
//初始化上下文中的事件機制
initApplicationEventMulticaster();
//初始化其他的特殊Bean
onRefresh();
//檢查監聽Bean并且将這些Bean向容器注冊
registerListeners();
//執行個體化所有的(non-lazy-init)單件
finishBeanFactoryInitialization(beanFactory);
//釋出容器事件,結束Refresh過程
finishRefresh();
}
catch (BeansException ex) {
logger.warn("Exception encountered during context initialization - cancelling refresh attempt", ex);
//為防止Bean資源占用,在異常進行中,銷毀已經在前面過程中生成的單件Bean
destroyBeans();
//重置active
cancelRefresh(ex);
// Propagate exception to caller.
throw ex;
}
}
}