一、引言
上篇博文,Huazie 带大家深入分析下 ApplicationArguments
接口及其默认实现。在初始化完 ApplicationArguments
之后,Spring Boot 就开始通过 prepareEnvironment
方法对 ConfigurableEnvironment
对象进行初始化操作。在介绍 ConfigurableEnvironment
的初始化之前,我们有必要先认识一下 ConfigurableEnvironment
接口。
二、主要内容
注意: 以下涉及 Spring Boot 源码 均来自版本 2.7.9,其他版本有所出入,可自行查看源码。
下面贴出 ConfigurableEnvironment
的源码:
java
代码解读
复制代码
public interface ConfigurableEnvironment extends Environment, ConfigurablePropertyResolver { void setActiveProfiles(String... profiles); void addActiveProfile(String profile); void setDefaultProfiles(String... profiles); MutablePropertySources getPropertySources(); Map<String, Object> getSystemProperties(); Map<String, Object> getSystemEnvironment(); void merge(ConfigurableEnvironment parent); }
从上述源码,可以看出 ConfigurableEnvironment
接口继承了 Environment
和 ConfigurablePropertyResolver
接口,它们其实最终都继承自 PropertyResolver
接口。
2.1 Environment
org.springframework.core.env.Environment
表示当前应用程序在其中运行的环境。它模拟了应用程序环境的两个关键方面:
2.1.1 配置文件(profiles)
Profile
是一