configurePropertySources
SpringApplication的configurePropertySources方法用于在此应用程序的环境中添加、删除或重新排序任何PropertySource。
protected void configurePropertySources(ConfigurableEnvironment environment, String[] args) {
MutablePropertySources sources = environment.getPropertySources();
if (!CollectionUtils.isEmpty(this.defaultProperties)) {
DefaultPropertiesPropertySource.addOrMerge(this.defaultProperties, sources);
}
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));
}
}
}
由于我引入了spring-boot-starter-webflux依赖,environment的类型为ApplicationReactiveWebEnvironment,会去调用AbstractEnvironment的getPropertySources()方法
getPropertySources()方法将会返回AbstractEnvironment的成员字段propertySources
private final MutablePropertySources propertySources;
public MutablePropertySources getPropertySources() {
return this.propertySources;
}
MutablePropertySources是PropertySources接口的默认实现,允许操作包含的属性源并提供用于复制现有PropertySources实例的构造函数。
DefaultPropertiesPropertySource是Environment中的最后一个属性源。DefaultPropertiesPropertySource继承MapPropertySource,MapPropertySource是从Map对象读取键和值的PropertySource。
DefaultPropertiesPropertySource的addOrMerge方法用于添加新的DefaultPropertiesPropertySource或与现有的合并。
public static void addOrMerge(Map<String, Object> source, MutablePropertySources sources) {
if (!CollectionUtils.isEmpty(source)) {
Map<String, Object> resultingSource = new HashMap<>();
DefaultPropertiesPropertySource propertySource = new DefaultPropertiesPropertySource(resultingSource);
if (sources.contains(NAME)) {
mergeIfPossible(source, sources, resultingSource);
sources.replace(NAME, propertySource);
}
else {
resultingSource.putAll(source);
sources.addLast(propertySource);
}
}
}
在SpringApplication执行完构造方法后去调用run方法时,成员字段defaultProperties为null,调用configurePropertySources方法并不会执行DefaultPropertiesPropertySource的addOrMerge方法。
在main启动时如果有传入的args参数,则会触发CompositeProperty的addPropertySource操作,CompositeProperty迭代一组PropertySource实例的复合PropertySource实现(在多个属性源共享相同名称的情况下是必需的)。
CompositeProperty的构造方法:
public CompositePropertySource(String name) {
super(name);
}
CompositeProperty的构造方法最终调用的是PropertySource的构造方法:
/**
* 使用给定的名称和源对象创建一个新的PropertySource 。
* @param name 关联的名称
* @param source 源对象
*/
public PropertySource(String name, T source) {
Assert.hasText(name, "Property source name must contain at least one character");
Assert.notNull(source, "Property source must not be null");
this.name = name;
this.source = source;
}
SimpleCommandLinePropertySource用于提供最简单的方法来解析命令行参数:
public class SimpleCommandLinePropertySource extends CommandLinePropertySource<CommandLineArgs> {
/**
* 创建一个新的SimpleCommandLinePropertySource具有默认名称并由给定的命令行参数String[]支持。
* @see CommandLinePropertySource#COMMAND_LINE_PROPERTY_SOURCE_NAME
* @see CommandLinePropertySource#CommandLinePropertySource(Object)
*/
public SimpleCommandLinePropertySource(String... args) {
super(new SimpleCommandLineArgsParser().parse(args));
}
/**
* 创建一个具有给定名称并由命令行参数的给定String[]支持的新SimpleCommandLinePropertySource 。
*/
public SimpleCommandLinePropertySource(String name, String[] args) {
super(name, new SimpleCommandLineArgsParser().parse(args));
}
/**
* 获取选项参数的属性名称。
*/
@Override
public String[] getPropertyNames() {
return StringUtils.toStringArray(this.source.getOptionNames());
}
@Override
protected boolean containsOption(String name) {
return this.source.containsOption(name);
}
@Override
@Nullable
protected List<String> getOptionValues(String name) {
return this.source.getOptionValues(name);
}
@Override
protected List<String> getNonOptionArgs() {
return this.source.getNonOptionArgs();
}
}
SimpleCommandLinePropertySource的构造方法最终调用的也是PropertySource的构造方法。
configureProfiles
protected void configureProfiles(ConfigurableEnvironment environment, String[] args) {
}