prepareRefresh方法源码跟踪
看这篇文章之前可以先了解之前的跟踪流程,https://www.jianshu.com/p/4934233f0ead
代码过宽,可以shift + 鼠标滚轮 左右滑动查看
AbstractApplicationContext类中refresh()方法的第一个调用方法prepareRefresh()的跟踪。
@Override
public void refresh() throws BeansException, IllegalStateException {
synchronized (this.startupShutdownMonitor) {
// Prepare this context for refreshing.
// 准备上下文的刷新
prepareRefresh();
···
}
直接进入。
prepareRefresh(零)
prepareRefresh方法在AbstractApplicationContext类中,此类是XmlWebApplicationContext的上层父类
// Prepare this context for refreshing.
// 准备 context 的刷新
prepareRefresh();
/**
* Prepare this context for refreshing, setting its startup date and
* active flag as well as performing any initialization of property sources.
*
* 准备 context 的刷新,设置他的启动数据、激活标志以及执行一些属性源的初始化
*/
protected void prepareRefresh() {
// 启动时间
this.startupDate = System.currentTimeMillis();
// 此 context 是否已经被关闭
this.closed.set(false);
// active 表示这个 context 当前是否处于活跃状态
this.active.set(true);
if (logger.isInfoEnabled()) {
logger.info("Refreshing " + this);
}
// Initialize any placeholder property sources in the context environment
// 1.在 context 的 environment 中初始化占位符属性源
initPropertySources();
// Validate that all properties marked as required are resolvable
// see ConfigurablePropertyResolver#setRequiredProperties
// 验证所有被标记为必要的属性是否可解析,如果有遗失属性则不能解析并抛出异常,
// 可以参考setRequiredProperties方法
getEnvironment().validateRequiredProperties();
// Allow for the collection of early ApplicationEvents,
// to be published once the multicaster is available...
// 一旦有可利用的广播,允许早期的ApplicationEvents集合被发布
this.earlyApplicationEvents = new LinkedHashSet<ApplicationEvent>();
}
1.initPropertySources
跟踪标记1的方法
initPropertySources 方法在 AbstractApplicationContext 中默认不做任何事情,具体业务流程由其子类覆盖完成,也就是在 AbstractRefreshableWebApplicationContext 类中进行,AbstractRefreshableWebApplicationContext 是XmlWebApplicationContext的父类
// Initialize any placeholder property sources in the context environment
// 1.在 context environment 中初始化占位符属性源
initPropertySources();
/**
* {@inheritDoc}
* <p>Replace {@code Servlet}-related property sources.
*
* 替换Servlet相关的属性源
*/
@Override
protected void initPropertySources() {
// 在设置 root web application context 的 contextConfigLocation 属性时,已经调用过此方法。
// 在ContextLoader类中的configureAndRefreshWebApplicationContext方法中,
// 又调用了 initPropertySources 方法对 environment 进行了初始化
// 此时 environment 已存在,直接返回。
ConfigurableEnvironment env = getEnvironment();
if (env instanceof ConfigurableWebEnvironment) {
// 1.1初始化属性源
((ConfigurableWebEnvironment) env).initPropertySources(this.servletContext, this.servletConfig);
}
}
1.1 initPropertySources
跟踪标记1.1的方法
env(也就是environment对象)调用了initPropertySources方法,该方法的具体实现由ConfigurableWebEnvironment的子类StandardServletEnvironment进行。
environment对象是StandardServletEnvironment类的实例。
//1.1初始化属性源
((ConfigurableWebEnvironment) env).initPropertySources(this.servletContext, this.servletConfig);
/**
* Replace any {@linkplain
* org.springframework.core.env.PropertySource.StubPropertySource stub property source}
* instances acting as placeholders with real servlet context/config property sources
* using the given parameters.
*
* 用真正的servlet context/config 属性源来替换作为占位符存在的 stub 属性源
*/
@Override
public void initPropertySources(ServletContext servletContext, ServletConfig servletConfig) {
// 工具类来初始化属性源,进入这个方法
WebApplicationContextUtils.initServletPropertySources(getPropertySources(), servletContext, servletConfig);
}
/**
* Replace {@code Servlet}-based {@link StubPropertySource stub property sources} with
* actual instances populated with the given {@code servletContext} and
* {@code servletConfig} objects.
* <p>This method is idempotent with respect to the fact it may be called any number
* of times but will perform replacement of stub property sources with their
* corresponding actual property sources once and only once.
*
* 用<由servletContext和servletConfig对象填充的>实例去替换基于servlet的 stub 属性源。
* 这个方法幂等遵守这个情况:他可能被调用任意次数,但是有且仅会执行一次 stub 属性源与它所匹配
* 的实际属性源的替换
*/
public static void initServletPropertySources(
MutablePropertySources propertySources, ServletContext servletContext, ServletConfig servletConfig) {
Assert.notNull(propertySources, "'propertySources' must not be null");
// 此条件第三项不通过。
// 在ContextLoader类的configureAndRefreshWebApplicationContext方法中
// 已调用过一次StandardServletEnvironment对象的initPropertySources方法,
// 完成了根属性源的替换,所以第三项不再通过
if (servletContext != null && propertySources.contains(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME) &&
propertySources.get(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME) instanceof StubPropertySource) {
propertySources.replace(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME,
new ServletContextPropertySource(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME, servletContext));
}
//此处servletConfig为空,所以跳过
if (servletConfig != null && propertySources.contains(StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME) &&
propertySources.get(StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME) instanceof StubPropertySource) {
propertySources.replace(StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME,
new ServletConfigPropertySource(StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME, servletConfig));
}
}
prepareRefresh方法只有这么多,接下来跟踪obtainFreshBeanFactory方法:
https://www.jianshu.com/p/144af98965d9
总结
- 设置 context 关闭状态、激活标志等
- 在 context 的 environment 中初始化占位符属性源,用真正的servlet context/config 属性源来替换作为占位符存在的 stub 属性源
- 验证所有被标记为必要的属性是否可解析
prepareRefresh方法源码跟踪的更多相关文章
- prepareRefresh()方法源码探究
该方法目的是做刷新上下文前的准备工作: 首先清空bean扫描器map中的内容,然后调用父类的prepareRefresh方法: 父类的准备刷新方法,主要做了3个工作: 1.简单的标志赋值----> ...
- loadBeanDefinitions方法源码跟踪(一)
看这篇文章之前可以先了解之前的跟踪流程,https://www.jianshu.com/p/4934233f0ead 代码过宽,可以shift + 鼠标滚轮 左右滑动查看 AbstractBeanDe ...
- postProcessBeanFactory方法源码跟踪
看这篇文章之前可以先了解之前的跟踪流程,https://www.jianshu.com/p/4934233f0ead 代码过宽,可以shift + 鼠标滚轮 左右滑动查看 AbstractApplic ...
- prepareBeanFactory方法源码跟踪
看这篇文章之前可以先了解之前的跟踪流程,https://www.jianshu.com/p/4934233f0ead 代码过宽,可以shift + 鼠标滚轮 左右滑动查看 AbstractApplic ...
- loadBeanDefinitions方法源码跟踪(三)
因为字数超过了限制,所以分成了三篇,承接上篇: https://www.jianshu.com/p/46e27afd7d96 代码过宽,可以shift + 鼠标滚轮 左右滑动查看 4.parseCus ...
- obtainFreshBeanFactory方法源码跟踪
看这篇文章之前可以先了解之前的跟踪流程,https://www.jianshu.com/p/4934233f0ead 代码过宽,可以shift + 鼠标滚轮 左右滑动查看 AbstractApplic ...
- loadBeanDefinitions方法源码跟踪(二)
因为字数超过了限制,所以分成了三篇,承接上篇: https://www.jianshu.com/p/a0cfaedf3fc5 代码过宽,可以shift + 鼠标滚轮 左右滑动查看 3.parseDef ...
- erlang下lists模块sort(排序)方法源码解析(一)
排序算法一直是各种语言最简单也是最复杂的算法,例如十大经典排序算法(动图演示)里面讲的那样 第一次看lists的sort方法的时候,蒙了,几百行的代码,我心想要这么复杂么(因为C语言的冒泡排序我记得不 ...
- Java源码跟踪阅读技巧
转:https://www.jianshu.com/p/ab865109070c 本文基于Eclipse IDE 1.Quick Type Hierarchy 快速查看类继承体系. 快捷键:Ctrl ...
随机推荐
- sql server删除重复的数据保留一条
DELETE FROM [TCX_1710_SHZJ].[dbo].[PR_BindingTray] WHERE 1=1 AND SNum in (SELECT * FROM ( (SELECT SN ...
- Spring SpringMVC 和 Springboot 的关系(转载)
原文链接 spring boot就是一个大框架里面包含了许许多多的东西,其中spring就是最核心的内容之一,当然就包含spring mvc. spring mvc 是只是spring 处理web层请 ...
- 【转载】手把手教你使用Git(简单,实用)
手把手教你使用Git(简单,实用) 标签: git 2016年04月21日 20:51:45 1328人阅读 评论(0) 收藏 举报 一:Git是什么? Git是目前世界上最先进的分布式版本控制系统. ...
- Python学习第十五课——类的基本思想(实例化对象,类对象)
类的基本思想 类:把一类事物的相同的特征和动作整合到一起就是类类是一个抽象的概念 对象:就是基于类而创建的一个具体的事物(具体存在的)也是特征和动作整合到一块 对象写法 # 对象写法 def scho ...
- Linux引导过程!(重点)
1.开机自检:服务器开机后,将根据主板BIOS (基本输入系统),中的设置对CPU .内存.显卡.键盘等设备进行初步检测,并初始化部分硬件. 2.MBR 引导:当从本机硬盘中启动系统时,首先根据硬盘中 ...
- MySQL 之存储引擎与数据类型与数据约束
一.存储引擎场景 1.InnoDB 用于事务处理应用程序,支持外键和行级锁.如果应用对事物的完整性有比较高的要求,在并发条件下要求数据的一致性,数据操作除了插入和查询之外,还包括很多更新和删除操作,那 ...
- redis 之redis发布订阅
Redis 通过 PUBLISH . SUBSCRIBE 等命令实现了订阅与发布模式. 一个Publisher,多个Subscriber模型 如下图所示,可以作为消息队列或者消息通道 主要应用: 一个 ...
- 一个小白对auth的理解
---恢复内容开始--- PS:最近需要做一个验证用户权限的功能,在官方和百度看了下,发现大家都是用auth来做验证,官方有很多auth的使用教程,但是都不全面,我也提问了几个关于auth的问题 也没 ...
- LeetCode刷题笔记和想法(C++)
主要用于记录在LeetCode刷题的过程中学习到的一些思想和自己的想法,希望通过leetcode提升自己的编程素养 :p 高效leetcode刷题小诀窍(这只是目前对我自己而言的小方法,之后会根据自己 ...
- 第1节 storm编程:4、storm环境安装以及storm编程模型介绍
dataSource:数据源,生产数据的东西 spout:接收数据源过来的数据,然后将数据往下游发送 bolt:数据的处理逻辑单元.可以有很多个,基本上每个bolt都处理一部分工作,然后将数据继续往下 ...