这一章我们讲解容器创建中的第一步,刷新此容器,也就是263行的prepareRefresh方法
1、方法概览
// 此方法位于AbstractApplicationContext类
protected void prepareRefresh() {
// 记录容器初始化开始时间
this.startupDate = System.currentTimeMillis();
// 设置容器初始化未完成
this.closed.set(false);
// 设置容器初始化正在进行
this.active.set(true);
// 打印日志,没什么好说的 ̄□ ̄||
if(this.logger.isDebugEnabled()) {
if(this.logger.isTraceEnabled()) {
this.logger.trace("Refreshing " + this);
} else {
this.logger.debug("Refreshing " + this.getDisplayName());
}
}
// 此方法是给子类初始化一些属性用的,点进去看的话是一个空实现。
this.initPropertySources();
// 获取系统属性,并且验证系统属性。验证的方法大家自己去看看吧,这里会讲解一下获取Environment
this.getEnvironment().validateRequiredProperties();
// 创建Spring事件派发器容器
this.earlyApplicationEvents = new LinkedHashSet();
}
2、getEnvironment方法
//此方法位于AbstractApplicationContext类
public ConfigurableEnvironment getEnvironment() {
//如果没有的话就创建一个,然后保存进去- -
if(this.environment == null) {
this.environment = this.createEnvironment();
}
return this.environment;
}
//此方法位于AbstractApplicationContext类
protected ConfigurableEnvironment createEnvironment() {
return new StandardEnvironment();
}
其实进入这一步的时候,environment已经被实例化了,笔者忘记讲了,该打- -,environment是在容器初始化装备时调用setConfigLocations方法->resolvePath方法->getEnvironment方法创建的,有关于这一段的内容可以参见从小白的角度看Spring核心流程概览(XML版)-初始化容器前的准备工作
public void setConfigLocations(@Nullable String... locations) {
if(locations != null) {
Assert.noNullElements(locations, "Config locations must not be null");
this.configLocations = new String[locations.length];
for(int i = 0; i < locations.length; ++i) {
//进resolvePath方法
this.configLocations[i] = this.resolvePath(locations[i]).trim();
}
} else {
this.configLocations = null;
}
}
protected String resolvePath(String path) {
//在此处调用了创建Environment的方法,至于后面的解析配置文件路径就不看了,不是重点- -
return this.getEnvironment().resolveRequiredPlaceholders(path);
}
总结
第一个步骤就这么完了- -,好像也没做什么事- -至于Environment类的话,就是封装了一些key-value,你可以通过Environment的getProperty方法获取一些值,比如environment.getProperty("os.name") = windos 7 能获取到你电脑的系统版本。 environment.getSystemEnvironment() = {LOCALAPPDATA=C:\Users\Administrator\AppData\Local, PROCESSOR_LEVEL=6, FP_NO_HOST_CHECK=NO, USERDOMAIN=1HL8OGG4IHSHRVG, LOGONSERVER=\1HL8OGG4IHSHRVG, JAVA_HOME=C:\Program Files\Java\jdk1.8.0_172, SESSIONNAME=Console, ALLUSERSPROFILE=C:\ProgramData, PROCESSOR_ARCHITECTURE=AMD64, PSModulePath=C:\Windows\system32\WindowsPowerShell\v1.0\Modules, SystemDrive=C:, APPDATA=C:\Users\Administrator\AppData\Roaming, USERNAME=Administrator, windows_tracing_logfile=C:\BVTBin\Tests\installpackage\csilogfile.log, ProgramFiles(x86)=C:\Program Files (x86), CommonProgramFiles=C:\Program Files\Common Files, Path=C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0;C:\Program Files\Java\jdk1.8.0_172\bin;C:\Program Files\Java\jdk1.8.0_172\jre\bin;F:\mysql-5.6.39-winx64\bin;F:\Git\cmd, PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC, OS=Windows_NT, windows_tracing_flags=3, COMPUTERNAME=1HL8OGG4IHSHRVG, _DFX_INSTALL_UNSIGNED_DRIVER=1, PROCESSOR_REVISION=3d04, CLASSPATH=.;C:\Program Files\Java\jdk1.8.0_172\lib;C:\Program Files\Java\jdk1.8.0_172\lib\tools.jar, CommonProgramW6432=C:\Program Files\Common Files, ComSpec=C:\Windows\system32\cmd.exe, ProgramData=C:\ProgramData, ProgramW6432=C:\Program Files, HOMEPATH=\Users\Administrator, SystemRoot=C:\Windows, TEMP=C:\Users\ADMINI~1\AppData\Local\Temp, HOMEDRIVE=C:, PROCESSOR_IDENTIFIER=Intel64 Family 6 Model 61 Stepping 4, GenuineIntel, USERPROFILE=C:\Users\Administrator, TMP=C:\Users\ADMINI~1\AppData\Local\Temp, CommonProgramFiles(x86)=C:\Program Files (x86)\Common Files, ProgramFiles=C:\Program Files, PUBLIC=C:\Users\Public, NUMBER_OF_PROCESSORS=4, windir=C:\Windows} 能够获取到系统的环境变量等等。其他功能就自己看啦~