这个方法位于: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
- 获取环境信息并验证必要的属性
- 准备监听器和事件的容器
\t\t// 设置启动时间 设置标识位
\t\tthis.startupDate = System.currentTimeMillis();\t\t// 设置容器停止标识为false
\t\tthis.closed.set(false);\t\t// 设置容器激活标识为true
- spring|立体双扬+大电池,OPPOA57正式开售,追剧党闭眼入!
- spring|台灯、词典笔、学习机,在线教育都去卖货了?
- |面试必须要掌握的内容:多线程与Spring容器事务机制
- spring|刘强东一生最大的困惑,消费者为何更爱淘宝拼多多?
- Python|熬夜整理了70个Python经典实用练手项目(附源码)
- TCL|【Java面试题】大厂面试必问的Spring 问题你来问,我来答!
- 蚂蚁集团向国家信息安全漏洞共享平台上报spring重大漏洞,现已
- spring|【Java面试题】大厂面试必问的Spring 问题你来问,我来答!
- spring|这次是真的了!微软发布电脑管家,国内用户专供
- spring|听音乐用什么无线耳机好,真无线耳机上手快速指南