SpringBoot源码 | prepareEnvironment方法解析
- SpringBoot
- prepareEnvironment源码
- getOrCreateEnvironment
- configureEnvironment
- configurePropertySources
- configureProfiles
- ConfigurationPropertySources.attach
- DefaultPropertiesPropertySource.moveToEnd
- bindToSpringApplication
- convertEnvironment
- 总结
SpringBoot
在SpringBoot启动流程中,主要的两个阶段是初始化SpringApplication对象以及SpringApplication.run方法执行的内容,今天主要细讲的是SpringApplication.run中的准备环境的prepareEnvironment方法源码ConfigurableEnvironment environment = prepareEnvironment(listeners, bootstrapContext, applicationArguments)。prepareEnvironment的源码如下
prepareEnvironment源码
prepareEnvironment主要包含方法getOrCreateEnvironment、configureEnvironment、ConfigurationPropertySources.attach、DefaultPropertiesPropertySource.moveToEnd、bindToSpringApplication,这里我们先看一下对应的注释
private ConfigurableEnvironment prepareEnvironment(SpringApplicationRunListeners listeners,
DefaultBootstrapContext bootstrapContext, ApplicationArguments applicationArguments) {
// 创建和配置环境
ConfigurableEnvironment environment = getOrCreateEnvironment();
//配置 property sources 和 profiles
configureEnvironment(environment, applicationArguments.getSourceArgs());
//将environment.getPropertySources()放在第一个位置
ConfigurationPropertySources.attach(environment);
//运行监听器通知所有监听器环境准备完成
listeners.environmentPrepared(bootstrapContext, environment);
//Move the 'defaultProperties' property source so that it's the last source
DefaultPropertiesPropertySource.moveToEnd(environment);
Assert.state(!environment.containsProperty("spring.main.environment-prefix"),
"Environment prefix cannot be set via properties.");
//Bind the environment to the {@link SpringApplication}
bindToSpringApplication(environment);
//环境转换成StandardEnvironment
if (!this.isCustomEnvironment) {
environment = convertEnvironment(environment);
}
ConfigurationPropertySources.attach(environment);
//返回环境配置对象ConfigurableEnvironment
return environment;
}
getOrCreateEnvironment
官方注释Create and configure the environment 创建和配置环境,由于我们的是SERVLET类型,所以这里会返回ApplicationServletEnvironment对象
configureEnvironment
configureEnvironment方法主要是配置property sources or profiles,源码可以看到
configurePropertySources
继续跟进configurePropertySources方法可以看到官方注释Add, remove or re-order any {@link PropertySource}s in this application’s environment意为在此应用程序环境中添加、删除或重新排序任何{@link PropertySource},源码如下
其中方法environment.getPropertySources()主要是为了获取ApplicationServletEnvironment中的PropertySources,获取之后会判断Map defaultProperties;是否不为null,不为null则执行addOrMerge判断是需要新增或者合并DefaultPropertiesPropertySource
回到configurePropertySources方法向下执行,
这里的args是the application arguments (usually passed from a Java main method)从run方法带下来的参数,这里判断args的长度是否大于0,满足条件后判断当前sources中是否包含命令行参数,包含的话就需要用args中参数替换sources中对应name的参数,如果不包含则直接把args放在首位,
configureProfiles
我们再来看configureProfiles方法,可以看到
官方注释Configure which profiles are active (or active by default) for this application environment. Additional profiles may be activated during configuration file processing via the {@code spring.profiles.active} property.的意思也就是说在配置文件处理过程中,可以通过{@code-spring.profiles.active}属性激活其他配置文件。
具体的实现去查看ConfigFileApplicationListener类By default properties will be loaded from
‘application.properties’ and/or ‘application.yml’ files in the following locations属性内容会从application.properties或者application.yml加载,继续向下
ConfigurationPropertySources.attach
ConfigurationPropertySources.attach方法
主要是为了将ConfigurationPropertySource附加到指定Environment,遍历Environment管理的每个PropertySource到ConfigurationPropertySource同时允许PropertySourcesPropertyResolver调用使用ConfigurationPropertyName配置的属性名,同时放在首位,之后listeners.environmentPrepared完成环境准备
DefaultPropertiesPropertySource.moveToEnd
之后调用DefaultPropertiesPropertySource.moveToEnd将Move the ‘defaultProperties’ property source so that it’s the last source移动到source末尾
bindToSpringApplication
环境参数配置完成之后执行bindToSpringApplication将环境配置参数绑定到SpringApplication
convertEnvironment
如果isCustomEnvironment为false则将ConfigurableEnvironment 转换为application environment并且不直接解析配置文件属性的应用程序环境
最后再执行一次ConfigurationPropertySources.attach,完成之后返回环境配置对象ConfigurableEnvironment,至此prepareEnvironment方法执行完成。
总结
执行完成准备环境的prepareEnvironment方法之后,会继续执行容器上下文启动前的其他准备工作,后续继续研究。