spring|Spring 源码 之Spring IOC 容器 前戏准备工作

spring|Spring 源码 之Spring IOC 容器 前戏准备工作

这个方法位于:AbstractApplicationContext#refresh , 这个方法中总共有15个方法 , Spring源码的精髓就是这15个方法中 。
@Override
\tpublic void refresh() throws BeansException IllegalStateException {\t\tsynchronized (this.startupShutdownMonitor) {\t\t\t// Prepare this context for refreshing.
\t\t\t// 准备工作加载环境变量等操作
\t\t\t// 1、设置容器启动时间
\t\t\t// 2、设置停止状态为false
\t\t\t// 3、设置活跃状态为true
\t\t\t// 4、获取Environment对象 , 并设置属性值
\t\t\t// 5、设置监听器和事件的集合 , 模式为空的集合
\t\t\tprepareRefresh();\t\t\t// Tell the subclass to refresh the internal bean factory.
\t\t\t// 告诉子类刷新内部 bean 工厂 获取刷新bean的工厂: DefaultListableBeanFactory
\t\t\t// 并且加载BeanDefinition
\t\t\tConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();\t\t\t// Prepare the bean factory for use in this context.
\t\t\t// 准备BeanFactory 设置一些属性
\t\t\tprepareBeanFactory(beanFactory);\t\t\ttry {\t\t\t\t// Allows post-processing of the bean factory in context subclasses.
// 允许子类进行扩展BeanFactoryPostProcessor
\t\t\t\tpostProcessBeanFactory(beanFactory);\t\t\t\t// Invoke factory processors registered as beans in the context.
\t\t\t\t// 实例化并执行BeanFactoryPostProcessor
\t\t\t\tinvokeBeanFactoryPostProcessors(beanFactory);\t\t\t\t// Register bean processors that intercept bean creation.
\t\t\t\t// 实例化并注册BeanPostProcessor
\t\t\t\tregisterBeanPostProcessors(beanFactory);\t\t\t\t// Initialize message source for this context.
\t\t\t\t// 国际化设置
\t\t\t\tinitMessageSource();\t\t\t\t// Initialize event multicaster for this context.
\t\t\t\t// 实例化事件多播器
\t\t\t\tinitApplicationEventMulticaster();\t\t\t\t// Initialize other special beans in specific context subclasses.
\t\t\t\t// 初始化特定上下文子类中的其他特殊bean , web容器
\t\t\t\tonRefresh();\t\t\t\t// Check for listener beans and register them.
\t\t\t\t// 检查listener bean 并注册它们
\t\t\t\t// 注册监听器
\t\t\t\tregisterListeners();\t\t\t\t// Instantiate all remaining (non-lazy-init) singletons.
\t\t\t\t// 实例化所有剩余的(非惰性初始化)单例 。
\t\t\t\tfinishBeanFactoryInitialization(beanFactory);\t\t\t\t// Last step: publish corresponding event.
\t\t\t\t// 发布相应的事件
\t\t\t\tfinishRefresh();
\t\t\t\t\t\tcatch (BeansException ex) {\t\t\t\tif (logger.isWarnEnabled()) {
\t\t\t\t\tlogger.warn(\"Exception encountered during context initialization - \" +\t\t\t\t\t\t\t\"cancelling refresh attempt: \" + ex);
\t\t\t\t\t\t\t\t// Destroy already created singletons to avoid dangling resources.
\t\t\t\t// 销毁Bean
\t\t\t\tdestroyBeans();\t\t\t\t// Reset 'active' flag.
\t\t\t\t// 重置 active 标志
\t\t\t\tcancelRefresh(ex);\t\t\t\t// Propagate exception to caller.
\t\t\t\tthrow ex;
\t\t\t\t\t\tfinally {\t\t\t\t// Reset common introspection caches in Spring's core since we
\t\t\t\t// might not ever need metadata for singleton beans anymore...
\t\t\t\tresetCommonCaches();
\t\t\t
\t\t
\t

前戏准备 prepareRefresh 方法Spring的前戏准备大概就是做了以下几件事:

  • 设置容器的启动时间
  • 设置容器的停止状态为false
  • 设置容器的激活状态为true
  • 获取环境信息并验证必要的属性
  • 准备监听器和事件的容器
protected void prepareRefresh() {\t\t// Switch to active.
\t\t// 设置启动时间 设置标识位
\t\tthis.startupDate = System.currentTimeMillis();\t\t// 设置容器停止标识为false
\t\tthis.closed.set(false);\t\t// 设置容器激活标识为true