- 在 Springboot 源码分析—— 总纲 中介绍了
Springboot
启动的总过程,本文将详细解析其中的 prepareEnvironment()
方法。
prepareEnvironment()
方法主要是为了查找并设置配置文件信息到 environment
中。
1 prepareEnvironment():查找并设置配置文件信息
- 根据应用类型,创建应用环境:如得到系统的参数、
JVM
及 Servlet
等参数,等
- 将
defaultProperties
、commandLine
及 active-prifiles
属性加载到环境中
- 将配置文件加载到环境中
private ConfigurableEnvironment prepareEnvironment(SpringApplicationRunListeners listeners, ApplicationArguments applicationArguments) {
ConfigurableEnvironment environment = this.getOrCreateEnvironment();
this.configureEnvironment((ConfigurableEnvironment)environment, applicationArguments.getSourceArgs());
listeners.environmentPrepared((ConfigurableEnvironment)environment);
this.bindToSpringApplication((ConfigurableEnvironment)environment);
if (!this.isCustomEnvironment) {
environment = (new EnvironmentConverter(this.getClassLoader())).convertEnvironmentIfNecessary((ConfigurableEnvironment)environment, this.deduceEnvironmentClass());
}
ConfigurationPropertySources.attach((Environment)environment);
return (ConfigurableEnvironment)environment;
}
1.1 environmentPrepared():运行一些7个监听器,主要是ConfigFileApplicationListener
- 运行7监听器中的环境配置类,关键的是
ConfigFileApplicationListener
,来读取配置文件,其它六个为:
AnsiOutputApplicationListener
:不知道干啥
LoggingApplicationListener
:不知道干啥
ClasspathLoggingApplicationListener
:打日志
BackgroundPreinitializer
:不知道干啥
DelegatingApplicationListener
:不知道干啥
FileEncodingApplicationListener
:判断编解码是否强制
public void environmentPrepared(ConfigurableEnvironment environment) {
Iterator var2 = this.listeners.iterator();
while(var2.hasNext()) {
SpringApplicationRunListener listener = (SpringApplicationRunListener)var2.next();
listener.environmentPrepared