@Override
public void refresh() throws BeansException, IllegalStateException {synchronized (this.startupShutdownMonitor) {// Prepare this context for refreshing.//1、調用容器準備刷新的方法,獲取容器的當時時間,同時給容器設置同步標識prepareRefresh();// Tell the subclass to refresh the internal bean factory.//2、告訴子類啟動refreshBeanFactory()方法,Bean定義資源文件的載入從//子類的refreshBeanFactory()方法啟動ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();// Prepare the bean factory for use in this context.//3、為BeanFactory配置容器特性,例如類加載器、事件處理器等prepareBeanFactory(beanFactory);try {// Allows post-processing of the bean factory in context subclasses.//4、為容器的某些子類指定特殊的BeanPost事件處理器postProcessBeanFactory(beanFactory);// Invoke factory processors registered as beans in the context.//5、調用所有注冊的BeanFactoryPostProcessor的BeaninvokeBeanFactoryPostProcessors(beanFactory);// Register bean processors that intercept bean creation.//6、為BeanFactory注冊BeanPost事件處理器.//BeanPostProcessor是Bean后置處理器,用于監聽容器觸發的事件registerBeanPostProcessors(beanFactory);// Initialize message source for this context.//7、初始化信息源,和國際化相關.initMessageSource();// Initialize event multicaster for this context.//8、初始化容器事件傳播器.initApplicationEventMulticaster();// Initialize other special beans in specific context subclasses.//9、調用子類的某些特殊Bean初始化方法onRefresh();// Check for listener beans and register them.//10、為事件傳播器注冊事件監聽器.registerListeners();// Instantiate all remaining (non-lazy-init) singletons.//11、初始化所有剩余的單例BeanfinishBeanFactoryInitialization(beanFactory);// Last step: publish corresponding event.//12、初始化容器的生命周期事件處理器,并發布容器的生命周期事件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.//13、銷毀已創建的BeandestroyBeans();// Reset 'active' flag.//14、取消refresh操作,重置容器的同步標識。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...//15、重設公共緩存resetCommonCaches();}}
}