ConfigFileApplicationListener是专门用来加载application.properties/ymlbootstrap.properties/yml配置文件的监听器。

  1. public class ConfigFileApplicationListener
  2. implements EnvironmentPostProcessor, SmartApplicationListener, Ordered {
  3. private static final String DEFAULT_PROPERTIES = "defaultProperties";
  4. // Note the order is from least to most specific (last one wins)
  5. // 顺序加载,后面的覆盖前面的
  6. private static final String DEFAULT_SEARCH_LOCATIONS = "classpath:/,classpath:/config/,file:./,file:./config/";
  7. // 默认加载的文件名
  8. private static final String DEFAULT_NAMES = "application";
  9. //
  10. private static final Set<String> NO_SEARCH_NAMES = Collections.singleton(null);
  11. // 激活的profile
  12. public static final String ACTIVE_PROFILES_PROPERTY = "spring.profiles.active";
  13. //
  14. public static final String INCLUDE_PROFILES_PROPERTY = "spring.profiles.include";
  15. // 加载的配置文件的名称,默认就是 DEFAULT_NAMES
  16. public static final String CONFIG_NAME_PROPERTY = "spring.config.name";
  17. // 加载位置,默认DEFAULT_SEARCH_LOCATIONS下挨个挨个去加载
  18. public static final String CONFIG_LOCATION_PROPERTY = "spring.config.location";
  19. /**
  20. * The "config additional location" property name.
  21. */
  22. public static final String CONFIG_ADDITIONAL_LOCATION_PROPERTY = "spring.config.additional-location";
  23. //
  24. public static final int DEFAULT_ORDER = Ordered.HIGHEST_PRECEDENCE + 10;
  25. //
  26. public static final String APPLICATION_CONFIGURATION_PROPERTY_SOURCE_NAME = "applicationConfigurationProperties";
  27. private final DeferredLog logger = new DeferredLog();
  28. // 搜索的路径,多个“,”隔开
  29. private String searchLocations;
  30. // 加载的配置文件名
  31. private String names;
  32. // order
  33. private int order = DEFAULT_ORDER;
  34. }

环境准备好事件

  1. @Override
  2. public void postProcessEnvironment(ConfigurableEnvironment environment,
  3. SpringApplication application) {
  4. addPropertySources(environment, application.getResourceLoader());
  5. }
  6. protected void addPropertySources(ConfigurableEnvironment environment,
  7. ResourceLoader resourceLoader) {
  8. RandomValuePropertySource.addToEnvironment(environment);
  9. new Loader(environment, resourceLoader).load();
  10. }

这里接收环境准备完成事件处理两件事:

  1. 添加一个处理配置中诸如random.int这样的随机数生成的RandomValuePropertySource
  2. 加载配置文件,如bootstrap.properties,application.properties

    1. 根据阶段的不同,在执行父容器(springcloud bootstrap)时,加载的是bootstrap.properties
    2. 执行应用容器(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 names = (isFolder ? getSearchNames() : NO_SEARCH_NAMES); names.forEach( (name) -> load(location, name, profile, filterFactory, consumer)); }); } // 指定的配置文件名称 private Set getSearchNames() { // 根据spring.config.name属性确定文件名 if (this.environment.containsProperty(CONFIG_NAME_PROPERTY)) { // String property = this.environment.getProperty(CONFIG_NAME_PROPERTY); return asResolvedSet(property, null); } return asResolvedSet(ConfigFileApplicationListener.this.names, DEFAULT_NAMES); } `` 默认配置时,这里加载classpath:/application-{profile}.properties/yml`等配置文件。