
这个方法位于: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|纯白ITX来了,还支持GEAR 1高频内存!新款七彩虹B660迷你主板测试
- spring|iPhone14系列再次被确认:配色、屏幕、电池容量、价格基本清晰了
- 3D打印|源码系统和saas账号的区别
- Python|什么是二级分销模式?分销商城小程序开发源码解析
- spring|安全管理框架,用Shiro还是用Spring Security?
- spring|这价格值得抢!锐龙7-6800H游戏本,配满血RTX3060高分屏6999元
- spring|微软明星产品Internet Explorer, IE浏览器在26岁时停止了更新
- spring|全面发展不偏科 OPPO Reno8 Pro+或成轻旗舰新标杆
- spring|隐藏在直播平台背后的陷阱
- spring|父亲节不知道送什么?看看这些“适老化”的华为智能穿戴产品
