StandardServletEnvironment

StandardServletEnvironment 添加2个 添加servletConfigInitParams 添加servletContextInitParams



protected void customizePropertySources(MutablePropertySources propertySources) {
propertySources.addLast(new StubPropertySource(SERVLET_CONFIG_PROPERTY_SOURCE_NAME)); //添加servletConfigInitParams propertysource
propertySources.addLast(new StubPropertySource(SERVLET_CONTEXT_PROPERTY_SOURCE_NAME)); //添加servletContextInitParams propertysource
if (JndiLocatorDelegate.isDefaultJndiEnvironmentAvailable()) {
propertySources.addLast(new JndiPropertySource(JNDI_PROPERTY_SOURCE_NAME)); //jndiProperties
}
super.customizePropertySources(propertySources); // 添加systemEnvironment systemProperties
}

springboot添加propertySource过程_配置文件

StandardEnvironment

# 添加 systemProperties systemEnvironment
protected void customizePropertySources(MutablePropertySources propertySources) {
propertySources.addLast( //系统参数 (比如vm options)
new PropertiesPropertySource(SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME, getSystemProperties())); // addLast systemProperties
propertySources.addLast( // 环境变量
new SystemEnvironmentPropertySource(SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, getSystemEnvironment())); // addLast systemEnvironment
}

springboot添加propertySource过程_环境变量_02

SpringApplication

不一定有,启动的时候加了命令行参数

这里是addFirst,添加到第一个位置

protected void configurePropertySources(ConfigurableEnvironment environment, String[] args) {
MutablePropertySources sources = environment.getPropertySources();
if (this.defaultProperties != null && !this.defaultProperties.isEmpty()) {
sources.addLast(new MapPropertySource("defaultProperties", this.defaultProperties));
}
if (this.addCommandLineProperties && args.length > 0) { // 有命令行参数 比如--server.port=11003
String name = CommandLinePropertySource.COMMAND_LINE_PROPERTY_SOURCE_NAME;
if (sources.contains(name)) { // 判断有没有存在名称=name的PropertySource
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)); // 这里添加first
}
}
}


springboot添加propertySource过程_配置文件_03


# systemEnvironment

# StandardEnvironment添加 SystemEnvironmentPropertySource
propertySources.addLast( // 环境变量
new SystemEnvironmentPropertySource(SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, getSystemEnvironment())); // addLast systemEnvironment




ConfigFileApplicationListener
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
addPropertySources(environment, application.getResourceLoader());
}


protected void addPropertySources(ConfigurableEnvironment environment, ResourceLoader resourceLoader) {
RandomValuePropertySource.addToEnvironment(environment); // 添加RandomValuePropertySource
new Loader(environment, resourceLoader).load(); // 默认情况下 这里加载所有application开头的资源文件
}


// 添加RandomValuePropertySource 添加到systemEnvironment后面

springboot添加propertySource过程_spring_04

加载所有的application配置文件,每个配置文件1个propertysource 

springboot添加propertySource过程_配置文件_05