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

SpringApplication的prepareEnvironment方法:DefaultPropertiesPropertySource继承MapPropertySource,MapPropertySource包含直接贡献给Spri

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<

发布者:admin,转转请注明出处:http://www.yc00.com/web/1755498965a5235438.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信