准备环境 :
private ConfigurableEnvironment prepareEnvironment(SpringApplicationRunListeners listeners,
DefaultBootstrapContext bootstrapContext, ApplicationArguments applicationArguments) {
// Create and configure the environment
//如果当前有上下文环境信息,就获取并返回;没有就创建
//比如保存底层运行的环境变量
ConfigurableEnvironment environment = getOrCreateEnvironment();
//配置环境(拿到环境信息,命令行参数)
configureEnvironment(environment, applicationArguments.getSourceArgs());
//绑定环境信息
ConfigurationPropertySources.attach(environment);
//监听器调用 listener.environmentPrepared();通知所有的监听器当前环境准备完成
listeners.environmentPrepared(bootstrapContext, environment);
//将环境信息设置到类的最后(这个不用管)
DefaultPropertiesPropertySource.moveToEnd(environment);
//配置额外的Profiles,激活哪些环境(不用管)
configureAdditionalProfiles(environment);
//绑定工作(都是找到一些东西,把一些属性绑定到某个值里面)
bindToSpringApplication(environment);
//又是准备一些环境信息
if (!this.isCustomEnvironment) {
environment = new EnvironmentConverter(getClassLoader()).convertEnvironmentIfNecessary(environment,
deduceEnvironmentClass());
}
//准备完了后,把环境信息再次绑定
ConfigurationPropertySources.attach(environment);
return environment;
}
=================进入getOrCreateEnvironment()
private ConfigurableEnvironment getOrCreateEnvironment() {
if (this.environment != null) {
return this.environment;
}
switch (this.webApplicationType) {
case SERVLET: //如果是原生servlet编程
return new StandardServletEnvironment();
case REACTIVE: //如果是响应式编程
return new StandardReactiveWebEnvironment();
default:
return new StandardEnvironment();
}
}
====================进入configureEnvironment()
protected void configureEnvironment(ConfigurableEnvironment environment, String[] args) {
//给环境添加一些类型转换器
if (this.addConversionService) {
ConversionService conversionService = ApplicationConversionService.getSharedInstance();
environment.setConversionService((ConfigurableConversionService) conversionService);
}
//加载(读取)外部配置源
//即读取所有的配置源的配置属性值
configurePropertySources(environment, args);
//激活Profiles环境
configureProfiles(environment, args);
}
==========进入configurePropertySources()
protected void configurePropertySources(ConfigurableEnvironment environment, String[] args) {
//获取到所有配置源
MutablePropertySources sources = environment.getPropertySources();
DefaultPropertiesPropertySource.ifNotEmpty(this.defaultProperties, sources::addLast);
//命令行信息
if (this.addCommandLineProperties && args.length > 0) {
String name = CommandLinePropertySource.COMMAND_LINE_PROPERTY_SOURCE_NAME;
if (sources.contains(name)) {
PropertySource<?> source = sources.get(name);
CompositePropertySource composite = new CompositePropertySource(name);
composite.addPropertySource(
new SimpleCommandLinePropertySource("springApplicationCommandLineArgs", args));
composite.addPropertySource(source);
sources.replace(name, composite);
}
else {
sources.addFirst(new SimpleCommandLinePropertySource(args));
}
}
}
==============进入environmentPrepared()
void environmentPrepared(ConfigurableBootstrapContext bootstrapContext, ConfigurableEnvironment environment) {
doWithListeners("spring.boot.application.environment-prepared",
(listener) -> listener.environmentPrepared(bootstrapContext, environment));
}
所有配置源
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。