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方法之后,会继续执行容器上下文启动前的其他准备工作,后续继续研究。

相关文章
|
11月前
|
人工智能 Java 开发者
【Spring】原理解析:Spring Boot 自动配置
Spring Boot通过“约定优于配置”的设计理念,自动检测项目依赖并根据这些依赖自动装配相应的Bean,从而解放开发者从繁琐的配置工作中解脱出来,专注于业务逻辑实现。
2994 0
|
10月前
|
前端开发 Java 微服务
《深入理解Spring》:Spring、Spring MVC与Spring Boot的深度解析
Spring Framework是Java生态的基石,提供IoC、AOP等核心功能;Spring MVC基于其构建,实现Web层MVC架构;Spring Boot则通过自动配置和内嵌服务器,极大简化了开发与部署。三者层层演进,Spring Boot并非替代,而是对前者的高效封装与增强,适用于微服务与快速开发,而深入理解Spring Framework有助于更好驾驭整体技术栈。
|
10月前
|
XML JSON Java
【SpringBoot(三)】从请求到响应再到视图解析与模板引擎,本文带你领悟SpringBoot请求接收全流程!
Springboot专栏第三章,从请求的接收到视图解析,再到thymeleaf模板引擎的使用! 本文带你领悟SpringBoot请求接收到渲染的使用全流程!
644 3
|
11月前
|
Java 数据库 数据安全/隐私保护
Spring Boot四层架构深度解析
本文详解Spring Boot四层架构(Controller-Service-DAO-Database)的核心思想与实战应用,涵盖职责划分、代码结构、依赖注入、事务管理及常见问题解决方案,助力构建高内聚、低耦合的企业级应用。
1843 1
|
前端开发 Java 数据库连接
SpringBoot参数校验底层原理和实操。深度历险、深度解析(图解+秒懂+史上最全)
SpringBoot参数校验底层原理和实操。深度历险、深度解析(图解+秒懂+史上最全)
SpringBoot参数校验底层原理和实操。深度历险、深度解析(图解+秒懂+史上最全)
|
机器学习/深度学习 XML Java
【spring boot logback】日志logback格式解析
在 Spring Boot 中,Logback 是默认的日志框架,它支持灵活的日志格式配置。通过配置 logback.xml 文件,可以定义日志的输出格式、日志级别、日志文件路径等。
1998 5
|
Java 关系型数据库 数据库连接
Spring Boot项目集成MyBatis Plus操作PostgreSQL全解析
集成 Spring Boot、PostgreSQL 和 MyBatis Plus 的步骤与 MyBatis 类似,只不过在 MyBatis Plus 中提供了更多的便利功能,如自动生成 SQL、分页查询、Wrapper 查询等。
1085 2
|
Java 数据库连接 API
Java 对象模型现代化实践 基于 Spring Boot 与 MyBatis Plus 的实现方案深度解析
本文介绍了基于Spring Boot与MyBatis-Plus的Java对象模型现代化实践方案。采用Spring Boot 3.1.2作为基础框架,结合MyBatis-Plus 3.5.3.1进行数据访问层实现,使用Lombok简化PO对象,MapStruct处理对象转换。文章详细讲解了数据库设计、PO对象实现、DAO层构建、业务逻辑封装以及DTO/VO转换等核心环节,提供了一个完整的现代化Java对象模型实现案例。通过分层设计和对象转换,实现了业务逻辑与数据访问的解耦,提高了代码的可维护性和扩展性。
529 1
|
Java 数据库 开发者
Spring Boot 框架超级详细总结及长尾关键词应用解析
本文深入讲解Spring Boot框架的核心概念、功能特性及实际应用,涵盖自动配置、独立运行、starter依赖等优势。通过Web开发、微服务架构、批处理等适用场景分析,结合在线书店实战案例,演示项目初始化、数据库设计、分层架构实现全流程。同时探讨热部署、多环境配置、缓存机制与事务管理等高级特性,助你高效掌握Spring Boot开发技巧。代码示例详尽,适合从入门到进阶的学习者。
4344 0

推荐镜像

更多
  • DNS