ConfigFileApplicationListener是专门用来加载application.properties/yml
或bootstrap.properties/yml
配置文件的监听器。
public class ConfigFileApplicationListener
implements EnvironmentPostProcessor, SmartApplicationListener, Ordered {
private static final String DEFAULT_PROPERTIES = "defaultProperties";
// Note the order is from least to most specific (last one wins)
// 顺序加载,后面的覆盖前面的
private static final String DEFAULT_SEARCH_LOCATIONS = "classpath:/,classpath:/config/,file:./,file:./config/";
// 默认加载的文件名
private static final String DEFAULT_NAMES = "application";
//
private static final Set<String> NO_SEARCH_NAMES = Collections.singleton(null);
// 激活的profile
public static final String ACTIVE_PROFILES_PROPERTY = "spring.profiles.active";
//
public static final String INCLUDE_PROFILES_PROPERTY = "spring.profiles.include";
// 加载的配置文件的名称,默认就是 DEFAULT_NAMES
public static final String CONFIG_NAME_PROPERTY = "spring.config.name";
// 加载位置,默认DEFAULT_SEARCH_LOCATIONS下挨个挨个去加载
public static final String CONFIG_LOCATION_PROPERTY = "spring.config.location";
/**
* The "config additional location" property name.
*/
public static final String CONFIG_ADDITIONAL_LOCATION_PROPERTY = "spring.config.additional-location";
//
public static final int DEFAULT_ORDER = Ordered.HIGHEST_PRECEDENCE + 10;
//
public static final String APPLICATION_CONFIGURATION_PROPERTY_SOURCE_NAME = "applicationConfigurationProperties";
private final DeferredLog logger = new DeferredLog();
// 搜索的路径,多个“,”隔开
private String searchLocations;
// 加载的配置文件名
private String names;
// order
private int order = DEFAULT_ORDER;
}
环境准备好事件
@Override
public void postProcessEnvironment(ConfigurableEnvironment environment,
SpringApplication application) {
addPropertySources(environment, application.getResourceLoader());
}
protected void addPropertySources(ConfigurableEnvironment environment,
ResourceLoader resourceLoader) {
RandomValuePropertySource.addToEnvironment(environment);
new Loader(environment, resourceLoader).load();
}
这里接收环境准备完成事件处理两件事:
- 添加一个处理配置中诸如
random.int
这样的随机数生成的RandomValuePropertySource。 加载配置文件,如bootstrap.properties,application.properties
- 根据阶段的不同,在执行父容器(springcloud bootstrap)时,加载的是bootstrap.properties
执行应用容器(springboot)时,加载的是application.properties ```java // 加载配置文件 public void load() { this.profiles = Collections.asLifoQueue(new LinkedList
()); this.processedProfiles = new LinkedList<>(); this.activatedProfiles = false; this.loaded = new LinkedHashMap<>(); // 确定加载哪些profiles initializeProfiles(); while (!this.profiles.isEmpty()) { // 一个一个profile来 Profile profile = this.profiles.poll(); // 加载指定profile的配置文件,last load(profile, this::getPositiveProfileFilter, addToLoaded(MutablePropertySources::addLast, false)); this.processedProfiles.add(profile); } // 加载默认配置文件,放在first load(null, this::getNegativeProfileFilter, addToLoaded(MutablePropertySources::addFirst, true)); addLoadedPropertySources(); }
// 加载配置文件
private void load(Profile profile, DocumentFilterFactory filterFactory,
DocumentConsumer consumer) {
getSearchLocations().forEach((location) -> {
boolean isFolder = location.endsWith(“/“);
// 根据配置文件名查找
Set``
默认配置时,这里加载
classpath:/application-{profile}.properties/yml`等配置文件。