首页 > 编程语言 >SpringBoot源码 | prepareEnvironment方法解析

SpringBoot源码 | prepareEnvironment方法解析

时间:2022-12-09 14:05:44浏览次数:48  
标签:SpringBoot ConfigurableEnvironment environment application 源码 prepareEnvironment


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对象

SpringBoot源码 | prepareEnvironment方法解析_servlet

configureEnvironment

configureEnvironment方法主要是配置property sources or profiles,源码可以看到

SpringBoot源码 | prepareEnvironment方法解析_servlet_02

configurePropertySources

继续跟进configurePropertySources方法可以看到官方注释Add, remove or re-order any {@link PropertySource}s in this application’s environment意为在此应用程序环境中添加、删除或重新排序任何{@link PropertySource},源码如下

SpringBoot源码 | prepareEnvironment方法解析_java_03


其中方法environment.getPropertySources()主要是为了获取ApplicationServletEnvironment中的PropertySources,获取之后会判断Map defaultProperties;是否不为null,不为null则执行addOrMerge判断是需要新增或者合并DefaultPropertiesPropertySource

SpringBoot源码 | prepareEnvironment方法解析_servlet_04


回到configurePropertySources方法向下执行,

SpringBoot源码 | prepareEnvironment方法解析_spring_05


这里的args是the application arguments (usually passed from a Java main method)从run方法带下来的参数,这里判断args的长度是否大于0,满足条件后判断当前sources中是否包含命令行参数,包含的话就需要用args中参数替换sources中对应name的参数,如果不包含则直接把args放在首位,

configureProfiles

我们再来看configureProfiles方法,可以看到

SpringBoot源码 | prepareEnvironment方法解析_spring boot_06


官方注释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方法

SpringBoot源码 | prepareEnvironment方法解析_java_07


主要是为了将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末尾

SpringBoot源码 | prepareEnvironment方法解析_java_08

bindToSpringApplication

环境参数配置完成之后执行bindToSpringApplication将环境配置参数绑定到SpringApplication

SpringBoot源码 | prepareEnvironment方法解析_spring_09

convertEnvironment

如果isCustomEnvironment为false则将ConfigurableEnvironment 转换为application environment并且不直接解析配置文件属性的应用程序环境

SpringBoot源码 | prepareEnvironment方法解析_spring_10


最后再执行一次ConfigurationPropertySources.attach,完成之后返回环境配置对象ConfigurableEnvironment,至此prepareEnvironment方法执行完成。

总结

执行完成准备环境的prepareEnvironment方法之后,会继续执行容器上下文启动前的其他准备工作,后续继续研究。


标签:SpringBoot,ConfigurableEnvironment,environment,application,源码,prepareEnvironment
From: https://blog.51cto.com/u_10917175/5925127

相关文章