深入SpringBoot源码(六)configurePropertySources

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) {
	}
  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值