
environment是在printBanner之前就初始化好了, 更在context创建之前,
已经加载application-xxxx.properties, System.properties, System.environment ...
也可以自己监听应用启动 SpringApplicationRunListener事件, 完成自己的独特的配置加载方案
启动后调用listener.finished() 打印一些启动后的信息
prepareEnvironment()源码如下
private ConfigurableEnvironment prepareEnvironment(
SpringApplicationRunListeners listeners,
ApplicationArguments applicationArguments) {
// Create and configure the environment
ConfigurableEnvironment environment = getOrCreateEnvironment(); //加载标准目录下的配置文件 &profile
configureEnvironment(environment, applicationArguments.getSourceArgs());
//加载自定义的配置方案
listeners.environmentPrepared(environment);
if (!this.webEnvironment) {
environment = new EnvironmentConverter(getClassLoader())
.convertToStandardEnvironmentIfNecessary(environment);
}
return environment;
}
@ConfigurationProperties 使用的变量也是从environment中取的
CLI参数配置方式:
参考: