SpringBoot源码 | prepareEnvironment方法解析

简介: 本文主要讲述SpringBoot启动流程源码中的prepareEnvironment()方法

SpringBoot

在SpringBoot启动流程中,主要的两个阶段是初始化SpringApplication对象以及SpringApplication.run方法执行的内容,今天主要细讲的是SpringApplication.run中的准备环境的prepareEnvironment方法源码ConfigurableEnvironment environment = prepareEnvironment(listeners, bootstrapContext, applicationArguments)。prepareEnvironment的源码如下

prepareEnvironment源码

prepareEnvironment主要包含方法getOrCreateEnvironment、configureEnvironment、ConfigurationPropertySources.attach、DefaultPropertiesPropertySource.moveToEnd、bindToSpringApplication,这里我们先看一下对应的注释

privateConfigurableEnvironmentprepareEnvironment(SpringApplicationRunListenerslisteners,
DefaultBootstrapContextbootstrapContext, ApplicationArgumentsapplicationArguments) {
// 创建和配置环境ConfigurableEnvironmentenvironment=getOrCreateEnvironment();
//配置 property sources 和 profilesconfigureEnvironment(environment, applicationArguments.getSourceArgs());
//将environment.getPropertySources()放在第一个位置ConfigurationPropertySources.attach(environment);
//运行监听器通知所有监听器环境准备完成listeners.environmentPrepared(bootstrapContext, environment);
//Move the 'defaultProperties' property source so that it's the last sourceDefaultPropertiesPropertySource.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);
//环境转换成StandardEnvironmentif (!this.isCustomEnvironment) {
environment=convertEnvironment(environment);
    }
ConfigurationPropertySources.attach(environment);
//返回环境配置对象ConfigurableEnvironmentreturnenvironment;
}

getOrCreateEnvironment

官方注释Create and configure the environment 创建和配置环境,由于我们的是SERVLET类型,所以这里会返回ApplicationServletEnvironment对象

image.png

configureEnvironment

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

image.png

configurePropertySources

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

image.png

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

image.png

回到configurePropertySources方法向下执行,

image.png

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

configureProfiles

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

image.png

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

image.png

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

image.png

bindToSpringApplication

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

image.png

convertEnvironment

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

image.png

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

总结

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

相关文章
|
1天前
PandasTA 源码解析(二十三)
PandasTA 源码解析(二十三)
7 0
|
1天前
PandasTA 源码解析(二十二)(3)
PandasTA 源码解析(二十二)
5 0
|
1天前
PandasTA 源码解析(二十二)(2)
PandasTA 源码解析(二十二)
9 2
|
1天前
PandasTA 源码解析(二十二)(1)
PandasTA 源码解析(二十二)
6 0
|
1天前
PandasTA 源码解析(二十一)(4)
PandasTA 源码解析(二十一)
7 1
|
1月前
|
Java Linux
Springboot 解决linux服务器下获取不到项目Resources下资源
Springboot 解决linux服务器下获取不到项目Resources下资源
|
1月前
|
Java API Spring
SpringBoot项目调用HTTP接口5种方式你了解多少?
SpringBoot项目调用HTTP接口5种方式你了解多少?
97 2
|
1月前
|
前端开发 JavaScript Java
6个SpringBoot 项目拿来就可以学习项目经验接私活
6个SpringBoot 项目拿来就可以学习项目经验接私活
40 0
|
2月前
|
Java Maven 微服务
springboot项目开启远程调试-jar包
springboot项目开启远程调试-jar包
25 0
|
3天前
|
Java 关系型数据库 MySQL
保姆级教程——将springboot项目部署到阿里云服务器包含环境配置(小白包会)
本文档详细介绍了将SpringBoot项目部署到阿里云服务器的步骤。首先,通过Xshell连接服务器,使用公网IP地址。接着,下载JDK的Linux版本,使用XFTP上传并解压,配置环境变量。然后,安装MySQL 5.7,包括下载YUM源、安装、启动服务以及修改root密码和开启远程访问。最后,将SpringBoot项目打包成jar,上传至服务器,使用`java -jar`命令运行,通过`nohup`确保服务持续运行。配置安全组以允许远程访问。

推荐镜像

更多