深入SpringBoot源码(十)SpringApplication与Environment的绑定(上)

SpringApplication的prepareEnvironment方法:
在这里插入图片描述
DefaultPropertiesPropertySource继承MapPropertySource,MapPropertySource包含直接贡献给SpringApplication的默认属性。按照惯例, DefaultPropertiesPropertySource始终是Environment中的最后一个属性源。DefaultPropertiesPropertySource的moveToEnd方法移动“defaultProperties”属性源,使其成为给定ConfigurableEnvironment中的最后一个源。

	public static void moveToEnd(ConfigurableEnvironment environment) {
   
		moveToEnd(environment.getPropertySources());
	}

	/**
	 * Move the 'defaultProperties' property source so that it's the last source in the
	 * given {@link MutablePropertySources}.
	 * @param propertySources the property sources to update
	 */
	public static void moveToEnd(MutablePropertySources propertySources) {
   
		PropertySource<?> propertySource = propertySources.remove(NAME);
		if (propertySource != null) {
   
			propertySources.addLast(propertySource);
		}
	}

在这里插入图片描述
SpringApplication的bindToSpringApplication方法将环境绑定到SpringApplication:

	protected void bindToSpringApplication(ConfigurableEnvironment environment) {
   
		try {
   
			Binder.get(environment).bind("spring.main", Bindable.ofInstance(this));
		}
		catch (Exception ex) {
   
			throw new IllegalStateException("Cannot bind to SpringApplication", ex);
		}
	}

Binder是一个容器对象,它绑定来自一个或多个ConfigurationPropertySources的对象。Binder的get方法从指定的环境创建一个新的Binder实例:

	public static Binder get(Environment environment) {
   
		return get(environment, null);
	}

	public static Binder get(Environment environment, BindHandler defaultBindHandler) {
   
		Iterable<ConfigurationPropertySource> sources = ConfigurationPropertySources.get(environment);
		PropertySourcesPlaceholdersResolver placeholdersResolver = new PropertySourcesPlaceholdersResolver(environment);
		return new Binder(sources, placeholdersResolver, null, null, defaultBindHandler);
	}

Binder的构造方法:

	public Binder(Iterable<
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值