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<