// 1.准备上下文内容 // Prepare this context for refreshing. prepareRefresh();
// 2.获取刷新的bean工厂 // Tell the subclass to refresh the internal bean factory. ConfigurableListableBeanFactory beanFactory=obtainFreshBeanFactory();
// 3.准备上下文用到的bean工厂,如:类加载器,后置处理器 // Prepare the bean factory for use in this context. prepareBeanFactory(beanFactory);
try{ // 4.通过编程方式修改BeanFactory的配置,比如添加自定义的BeanDefinition,修改属性值,注册BeanPostProcessor等等。 // 通过这种方式,开发人员可以对Spring容器进行更灵活和定制化的配置 // Allows post-processing of the bean factory in context subclasses. postProcessBeanFactory(beanFactory);
// 5.执行所有已注册的BeanFactoryPostProcessor的postProcessBeanFactory方法 StartupStep beanPostProcess=this.applicationStartup.start("spring.context.beans.post-process"); // Invoke factory processors registered as beans in the context. invokeBeanFactoryPostProcessors(beanFactory);
// 7.初始化上下文的消息资源 // 消息资源用于国际化和本地化的目的,它可以根据不同的语言和区域设置,提供相应的文本消息 // Initialize message source for this context. initMessageSource();
// 8.初始化应用程序事件的多播器,使得应用程序能够对事件进行发布和监听 // 事件机制是一种通信机制,用于在不同的组件之间传递消息和触发相应的处理逻辑 // Initialize event multicaster for this context. initApplicationEventMulticaster();
// 9.容器刷新过程中的一个回调方法,用于提供一个扩展点,让开发人员可以在容器刷新完成后执行一些自定义的逻辑 // 可以在onRefresh方法中执行一些额外的初始化操作、启动定时任务、注册额外的bean等 // Initialize other special beans in specific context subclasses. onRefresh();
// 10.向应用程序上下文注册事件监听器 // Check for listener beans and register them. registerListeners();
// 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(); contextRefresh.end(); } } }