SpringBoot 扩展篇:ConfigFileApplicationListener源码解析

server/2024/10/21 9:32:56/

SpringBoot 扩展篇:ConfigFileApplicationListener源码解析

    • 1.概述
    • 2. ConfigFileApplicationListener定义
    • 3. ConfigFileApplicationListener回调链路
      • 3.1 SpringApplication#run
      • 3.2 SpringApplication#prepareEnvironment
      • 3.3 配置environment
    • 4. 环境准备事件 ConfigFileApplicationListener#onApplicationEvent
    • 4. 加载配置类
      • 4.1 Loader相关属性介绍
      • 4.2 Loader加载配置文件
        • FilteredPropertySource#apply
        • ConfigFileApplicationListener.Loader#load()
        • ConfigFileApplicationListener.Loader#initializeProfiles
        • Loader#addLoadedPropertySources
        • ConfigFileApplicationListener.Loader#getSearchLocations()
        • ConfigFileApplicationListener.Loader#load()
        • ConfigFileApplicationListener.Loader#load()
        • ConfigFileApplicationListener.Loader#loadForFileExtension
        • ConfigFileApplicationListener.Loader#load
      • 配置文件加载顺序总结
      • 问题:为什么先加入到environment中的propertySource,优先级越高?
      • 遗留问题:

1.概述

SpringBoot的配置文件加载由ConfigFileApplicationListener完成的,它会加载application.properties、application.yml等配置文件,还支持用户配置和扩展。本文从源码的角度分析它的原理

加载完毕的配置信息最终都会放入到Environment中。

2. ConfigFileApplicationListener定义

在这里插入图片描述
ConfigFileApplicationListener定义在spring.factories中。监听器注册和执行原理参考:SpringBoot 源码解析3:事件监听器

3. ConfigFileApplicationListener回调链路

3.1 SpringApplication#run

java">public ConfigurableApplicationContext run(String... args) {StopWatch stopWatch = new StopWatch();stopWatch.start();ConfigurableApplicationContext context = null;configureHeadlessProperty();SpringApplicationRunListeners listeners = getRunListeners(args);listeners.starting();try {ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments);configureIgnoreBeanInfo(environment);Banner printedBanner = printBanner(environment);context = createApplicationContext();prepareContext(context, environment, listeners, applicationArguments, printedBanner);refreshContext(context);afterRefresh(context, applicationArguments);stopWatch.stop();if (this.logStartupInfo) {new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch);}listeners.started(context);callRunners(context, applicationArguments);}catch (Throwable ex) {handleRunFailure(context, ex, listeners);throw new IllegalStateException(ex);}try {listeners.running(context);}catch (Throwable ex) {handleRunFailure(context, ex, null);throw new IllegalStateException(ex);}return context;
}

这是SpringBoot启动最基础的方法,调用了prepareEnvironment。

3.2 SpringApplication#prepareEnvironment

java">private ConfigurableEnvironment prepareEnvironment(SpringApplicationRunListeners listeners,ApplicationArguments applicationArguments) {// Create and configure the environment// 创建environment对象ConfigurableEnvironment environment = getOrCreateEnvironment();// 配置环境configureEnvironment(environment, applicationArguments.getSourceArgs());ConfigurationPropertySources.attach(environment);// 发布监听事件listeners.environmentPrepared(environment);bindToSpringApplication(environment);if (!this.isCustomEnvironment) {environment = new EnvironmentConverter(getClassLoader()).convertEnvironmentIfNecessary(environment,deduceEnvironmentClass());}ConfigurationPropertySources.attach(environment);return environment;
}
  1. getOrCreateEnvironment创建StandardServletEnvironment,所有的启动参数和配置文件信息都会保存到environment中。environment中默认创建了4个propertySource,分别用来存放系统属性和servlet属性。
    在这里插入图片描述

  2. configureEnvironment配置环境信息,此时配置文件还没解析。

  3. listeners.environmentPrepared,调用监听器ConfigFileApplicationListener解析配置文件。最终回调了ConfigFileApplicationListener#onApplicationEvent,这里是解析文件的核心逻辑。

java">@Override
public void environmentPrepared(ConfigurableEnvironment environment) {this.initialMulticaster.multicastEvent(new ApplicationEnvironmentPreparedEvent(this.application, this.args, environment));
}

监听器发布的是ApplicationEnvironmentPreparedEvent类型的事件。

  1. bindToSpringApplication解析完毕所有的配置文件信息之后,将spring.main.*的环境变量与当前的springApplication对象的属性绑定。比如allowBeanDefinitionOverriding配置就是在这里读取的。

3.3 配置environment

SpringApplication#configureEnvironment

java">protected void configureEnvironment(ConfigurableEnvironment environment, String[] args) {if (this.addConversionService) {ConversionService conversionService = ApplicationConversionService.getSharedInstance();environment.setConversionService((ConfigurableConversionService) conversionService);}configurePropertySources(environment, args);configureProfiles(environment, args);
}
  1. 第二个参数args为SpringBoot启动参数。
  2. configurePropertySources方法会将启动参数解析保存到environment中。
java">protected void configurePropertySources(ConfigurableEnvironment environment, String[] args) {MutablePropertySources sources = environment.getPropertySources();if (this.defaultProperties != null && !this.defaultProperties.isEmpty()) {sources.addLast(new MapPropertySource("defaultProperties", this.defaultProperties));}if (this.addCommandLineProperties && args.length > 0) {String name = CommandLinePropertySource.COMMAND_LINE_PROPERTY_SOURCE_NAME;if (sources.contains(name)) {PropertySource<?> source = sources.get(name);CompositePropertySource composite = new CompositePropertySource(name);composite.addPropertySource(new SimpleCommandLinePropertySource("springApplicationCommandLineArgs", args));composite.addPropertySource(source);sources.replace(name, composite);}else {sources.addFirst(new SimpleCommandLinePropertySource(args));}}
}

通过addFirst会将启动参数的属性添加到第一个PropertySources,优先级最高

java">protected <T> T getProperty(String key, Class<T> targetValueType, boolean resolveNestedPlaceholders) {if (this.propertySources != null) {for (PropertySource<?> propertySource : this.propertySources) {if (logger.isTraceEnabled()) {logger.trace("Searching for key '" + key + "' in PropertySource '" +propertySource.getName() + "'");}Object value = propertySource.getProperty(key);if (value != null) {if (resolveNestedPlaceholders && value instanceof String) {value = resolveNestedPlaceholders((String) value);}logKeyFound(key, propertySource, value);return convertValueIfNecessary(value, targetValueType);}}}if (logger.isTraceEnabled()) {logger.trace("Could not find key '" + key + "' in any property source");}return null;
}

如果有多个相同的key在不同的propertySource中,在通过key从environment中获取值的时候,会遍历所有的PropertySources,获取到第一个就会返回。
3. configureProfiles方法

java">protected void configureProfiles(ConfigurableEnvironment environment, String[] args) {Set<String> profiles = new LinkedHashSet<>(this.additionalProfiles);profiles.addAll(Arrays.asList(environment.getActiveProfiles()));environment.setActiveProfiles(StringUtils.toStringArray(profiles));
}

通过environment.getActiveProfiles() 获取spring.profiles.active的值,此时的配置文件还没有解析,获取到的是启动参数中的值。

4. 环境准备事件 ConfigFileApplicationListener#onApplicationEvent

由上文可知,发布的是ApplicationEnvironmentPreparedEvent类型的事件

java">@Override
public void onApplicationEvent(ApplicationEvent event) {if (event instanceof ApplicationEnvironmentPreparedEvent) {onApplicationEnvironmentPreparedEvent((ApplicationEnvironmentPreparedEvent) event);}if (event instanceof ApplicationPreparedEvent) {onApplicationPreparedEvent(event);}
}private void onApplicationEnvironmentPreparedEvent(ApplicationEnvironmentPreparedEvent event) {List<EnvironmentPostProcessor> postProcessors = loadPostProcessors();postProcessors.add(this);AnnotationAwareOrderComparator.sort(postProcessors);for (EnvironmentPostProcessor postProcessor : postProcessors) {postProcessor.postProcessEnvironment(event.getEnvironment(), event.getSpringApplication());}
}

loadPostProcessors会从spring.factories中加载所有EnvironmentPostProcessor类型的处理器。
SpringBoot 基础概念:SpringApplication#getSpringFactoriesInstances
最终将自己加入到这些处理器中,然后依次执行postProcessEnvironment方法。

4. 加载配置类

加载配置类的核心逻辑的入口在 ConfigFileApplicationListener#postProcessEnvironment。

java">@Override
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {addPropertySources(environment, application.getResourceLoader());
}
java">protected void addPropertySources(ConfigurableEnvironment environment, ResourceLoader resourceLoader) {RandomValuePropertySource.addToEnvironment(environment);new Loader(environment, resourceLoader).load();
}

可以看到,加载配置的逻辑交给了Loader。

4.1 Loader相关属性介绍

Loader是ConfigFileApplicationListener的内部类。

构造器

java">Loader(ConfigurableEnvironment environment, ResourceLoader resourceLoader) {this.environment = environment;this.placeholdersResolver = new PropertySourcesPlaceholdersResolver(this.environment);this.resourceLoader = (resourceLoader != null) ? resourceLoader : new DefaultResourceLoader();this.propertySourceLoaders = SpringFactoriesLoader.loadFactories(PropertySourceLoader.class,getClass().getClassLoader());
}

propertySourceLoaders 是从spring.factories文件中加载的配置文件加载器。
在这里插入图片描述
PropertiesPropertySourceLoader负责读取*.properties、*.xml中的内容

java">public class PropertiesPropertySourceLoader implements PropertySourceLoader {private static final String XML_FILE_EXTENSION = ".xml";@Overridepublic String[] getFileExtensions() {return new String[] { "properties", "xml" };}....
}

YamlPropertySourceLoader负责读取*.yml、*.yaml文件中的内容

java">public class YamlPropertySourceLoader implements PropertySourceLoader {@Overridepublic String[] getFileExtensions() {return new String[] { "yml", "yaml" };}
}

ConfigFileApplicationListener中的属性

java">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);private static final Bindable<String[]> STRING_ARRAY = Bindable.of(String[].class);private static final Bindable<List<String>> STRING_LIST = Bindable.listOf(String.class);private static final Set<String> LOAD_FILTERED_PROPERTY;static {Set<String> filteredProperties = new HashSet<>();filteredProperties.add("spring.profiles.active");filteredProperties.add("spring.profiles.include");LOAD_FILTERED_PROPERTY = Collections.unmodifiableSet(filteredProperties);
}

Loader类中的属性

java">private final Log logger = ConfigFileApplicationListener.this.logger;// environment,Spring所有解析的配置信息和启动参数都放入到了environment中 
private final ConfigurableEnvironment environment;// 占位符解析器,解析${key}
private final PropertySourcesPlaceholdersResolver placeholdersResolver;// 资源加载器,加载文件资源
private final ResourceLoader resourceLoader;private final List<PropertySourceLoader> propertySourceLoaders;// profile对应spring.profiles.active、spring.profiles.include、spring.profiles.default对应的配置属性
private Deque<Profile> profiles;private List<Profile> processedProfiles;private boolean activatedProfiles;private Map<Profile, MutablePropertySources> loaded;private Map<DocumentsCacheKey, List<Document>> loadDocumentsCache = new HashMap<>();

4.2 Loader加载配置文件

FilteredPropertySource#apply
java">static void apply(ConfigurableEnvironment environment, String propertySourceName, Set<String> filteredProperties,Consumer<PropertySource<?>> operation) {MutablePropertySources propertySources = environment.getPropertySources();PropertySource<?> original = propertySources.get(propertySourceName);// 判断environment中是否有名称为"defaultProperties"的资源if (original == null) {// 如果没有defaultProperties资源,那么就回调Loader类中的Consumer方法operation.accept(null);return;}// 如果有defaultProperties资源,就封装成FilteredPropertySourcepropertySources.replace(propertySourceName, new FilteredPropertySource(original, filteredProperties));try {// 回调Loader类中的Consumer方法operation.accept(original);}finally {// 替换PropertySourcepropertySources.replace(propertySourceName, original);}
}

如果environment中没有名称为“defaultProperties”属性资源,那么就直接回调Loader中的Consumer方法 (defaultProperties) -> { … } ,参数为null。

Springboot默认是没有defaultProperties的
在这里插入图片描述

ConfigFileApplicationListener.Loader#load()

对加载配置文件时所需的属性初始化。

java">void load() {FilteredPropertySource.apply(this.environment, DEFAULT_PROPERTIES, LOAD_FILTERED_PROPERTY,(defaultProperties) -> {this.profiles = new LinkedList<>();this.processedProfiles = new LinkedList<>();this.activatedProfiles = false;this.loaded = new LinkedHashMap<>();// 初始化profileinitializeProfiles();while (!this.profiles.isEmpty()) {Profile profile = this.profiles.poll();if (isDefaultProfile(profile)) {addProfileToEnvironment(profile.getName());}// 加载配置文件load(profile, this::getPositiveProfileFilter,addToLoaded(MutablePropertySources::addLast, false));this.processedProfiles.add(profile);}load(null, this::getNegativeProfileFilter, addToLoaded(MutablePropertySources::addFirst, true));// 将加载到的PropertySources放入到environment的最后addLoadedPropertySources();// 将所有加载到了的profiles,设置到environment中applyActiveProfiles(defaultProperties);});
}
  1. (defaultProperties) -> { … } 是一个函数接口 Consumer,所以需要先看 FilteredPropertySource#apply方法,在apply方法内部回调这个Consumer。
  2. initializeProfiles:初始化profile。
  3. profiles.poll先入先出,依次加载profile,后续的配置文件中有profile,也会放入到profiles中。
  4. addLoadedPropertySources方法,在profiles循环完毕,所有配置加载完毕,将读取到的内容添加到environment中。
ConfigFileApplicationListener.Loader#initializeProfiles

初始化profile。日常工作中profile指的是dev、uat、prod等配置,但是我们的思维不要局限于这里。

java">private void initializeProfiles() {// The default profile for these purposes is represented as null. We add it// first so that it is processed first and has lowest priority.// 1. 添加一个为null的profilethis.profiles.add(null);// 获取spring.profiles.activeSet<Profile> activatedViaProperty = getProfilesFromProperty(ACTIVE_PROFILES_PROPERTY);// 获取spring.profiles.includeSet<Profile> includedViaProperty = getProfilesFromProperty(INCLUDE_PROFILES_PROPERTY);// 获取不在当前spring.profiles.active和spring.profiles.include范围内,并且之前获取到的spring.profiles.active(环境)List<Profile> otherActiveProfiles = getOtherActiveProfiles(activatedViaProperty, includedViaProperty);// 2. 添加不在当前spring.profiles.active和spring.profiles.include范围内的,之前获取到的spring.profiles.activethis.profiles.addAll(otherActiveProfiles);// Any pre-existing active profiles set via property sources (e.g.// System properties) take precedence over those added in config files.// 3. 添加spring.profiles.include对应的profilethis.profiles.addAll(includedViaProperty);// 4. 添加spring.profiles.active对应的profileaddActiveProfiles(activatedViaProperty);// 5. 如果没有spring.profiles.active和spring.profiles.include,那么就使用spring.profiles.defaultif (this.profiles.size() == 1) { // only has null profilefor (String defaultProfileName : this.environment.getDefaultProfiles()) {Profile defaultProfile = new Profile(defaultProfileName, true);this.profiles.add(defaultProfile);}}
}

profiles添加的优先顺序,决定了profile加载的顺序,先进先出

  1. 添加一个为null的profile。因为就算用户配置了spring.profiles.active=dev,不仅要加载application-dev.yml文件,application.yml文件也需要被加载。
  2. 添加不在当前spring.profiles.active和spring.profiles.include范围内的,之前获取到的spring.profiles.active。前期在SpringBoot启动的时候在SpringApplication#configureProfiles方法中就已经获取到了启动参数中的spring.profiles.active。
  3. 添加spring.profiles.include对应的profile。
  4. 添加spring.profiles.active对应的profile。
  5. 如果没有spring.profiles.active和spring.profiles.include,那么就使用spring.profiles.default对应的profile。
Loader#addLoadedPropertySources

将配置文件中加载的属性放入到environment中。

java">private void addLoadedPropertySources() {MutablePropertySources destination = this.environment.getPropertySources();List<MutablePropertySources> loaded = new ArrayList<>(this.loaded.values());Collections.reverse(loaded);String lastAdded = null;Set<String> added = new HashSet<>();for (MutablePropertySources sources : loaded) {for (PropertySource<?> source : sources) {if (added.add(source.getName())) {addLoadedPropertySource(destination, lastAdded, source);lastAdded = source.getName();}}}
}

loaded存放的是已经加载过的属性,它是一个LinkedHashMap,key为profile,value为propertySource,一个propertySource对应一个配置文件。会将profile加载的顺序颠倒,通过addLoadedPropertySource添加到environment中。在environment中,先加入的property,优先级越高
所以,后加载的profile,优先级就越高。这也就是为什么上述Loader#initializeProfiles方法中this.profiles.add(null)这行代码的意义,就是为了将没有profile的文件的优先级降到最低。

ConfigFileApplicationListener.Loader#getSearchLocations()
java">private Set<String> getSearchLocations() {if (this.environment.containsProperty(CONFIG_LOCATION_PROPERTY)) {return getSearchLocations(CONFIG_LOCATION_PROPERTY);}Set<String> locations = getSearchLocations(CONFIG_ADDITIONAL_LOCATION_PROPERTY);locations.addAll(asResolvedSet(ConfigFileApplicationListener.this.searchLocations, DEFAULT_SEARCH_LOCATIONS));return locations;
}

获取文件父路径

  1. spring.config.location强制指定文件路径,只能从这个路径下面寻找文件
  2. spring.config.additional-location额外的文件查找路径
  3. 默认了四个路径:classpath:/,classpath:/config/,file:./,file:./config/,在asResolvedSet方法中将顺序颠倒了。
  4. locations中可配置文件父路径,也可能是文件的绝对路径。
ConfigFileApplicationListener.Loader#load()
java">private void load(Profile profile, DocumentFilterFactory filterFactory, DocumentConsumer consumer) {getSearchLocations().forEach((location) -> {boolean isFolder = location.endsWith("/");Set<String> names = isFolder ? getSearchNames() : NO_SEARCH_NAMES;names.forEach((name) -> load(location, name, profile, filterFactory, consumer));});
}
  1. 先遍历所有的locations,如果location为文件,那么就直接通过location加载配置。如果为文件夹,那么就查询获取文件名称,通过文件夹+文件名称去加载文件。
  2. getSearchNames() : 获取文件名称,可通过spring.config.name配置文件名称,没有配置则使用"application"。这也解释了SpringBoot启动的时候,为什么回去加载application.yml文件。
java">private Set<String> getSearchNames() {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);
}
ConfigFileApplicationListener.Loader#load()
java">private void load(String location, String name, Profile profile, DocumentFilterFactory filterFactory,DocumentConsumer consumer) {if (!StringUtils.hasText(name)) {for (PropertySourceLoader loader : this.propertySourceLoaders) {if (canLoadFileExtension(loader, location)) {load(loader, location, profile, filterFactory.getDocumentFilter(profile), consumer);return;}}throw new IllegalStateException("File extension of config file location '" + location+ "' is not known to any PropertySourceLoader. If the location is meant to reference "+ "a directory, it must end in '/'");}Set<String> processed = new HashSet<>();for (PropertySourceLoader loader : this.propertySourceLoaders) {for (String fileExtension : loader.getFileExtensions()) {if (processed.add(fileExtension)) {loadForFileExtension(loader, location + name, "." + fileExtension, profile, filterFactory,consumer);}}}
}

location可能为文件的全路径(spring.config.additional-location配置),为全路径则文件名称name为null。分成了两种方式加载文件,其实两种方式的逻辑是一样的。我们关注下半部分location + name, “.” + fileExtension加载方式。
在这里插入图片描述
先使用Properties加载器,在使用Yaml加载器。这就是为什么properties文件优先级高于yaml文件的原因。

ConfigFileApplicationListener.Loader#loadForFileExtension

通过文件的扩展名称加载

java">private void loadForFileExtension(PropertySourceLoader loader, String prefix, String fileExtension,Profile profile, DocumentFilterFactory filterFactory, DocumentConsumer consumer) {DocumentFilter defaultFilter = filterFactory.getDocumentFilter(null);DocumentFilter profileFilter = filterFactory.getDocumentFilter(profile);if (profile != null) {// Try profile-specific file & profile section in profile file (gh-340)String profileSpecificFile = prefix + "-" + profile + fileExtension;load(loader, profileSpecificFile, profile, defaultFilter, consumer);load(loader, profileSpecificFile, profile, profileFilter, consumer);// Try profile specific sections in files we've already processedfor (Profile processedProfile : this.processedProfiles) {if (processedProfile != null) {String previouslyLoaded = prefix + "-" + processedProfile + fileExtension;load(loader, previouslyLoaded, profile, profileFilter, consumer);}}}// Also try the profile-specific section (if any) of the normal fileload(loader, prefix + fileExtension, profile, profileFilter, consumer);
}
  1. prefix为文件夹路径+文件名称,比如:file:./config/application
  2. 如果profile不为空,那么就使用prefix + “-” + profile + fileExtension加载,比如:file:./config/application-dev.yml。
  3. 最后,不管profile是否为空,都会通过prefix + fileExtension加载,比如:file:./config/application.yml。
ConfigFileApplicationListener.Loader#load
java">private void load(PropertySourceLoader loader, String location, Profile profile, DocumentFilter filter,DocumentConsumer consumer) {try {// 判断文件资源是否存在Resource resource = this.resourceLoader.getResource(location);if (resource == null || !resource.exists()) {if (this.logger.isTraceEnabled()) {StringBuilder description = getDescription("Skipped missing config ", location, resource,profile);this.logger.trace(description);}return;}// 校验文件扩展名称不为空if (!StringUtils.hasText(StringUtils.getFilenameExtension(resource.getFilename()))) {if (this.logger.isTraceEnabled()) {StringBuilder description = getDescription("Skipped empty config extension ", location,resource, profile);this.logger.trace(description);}return;}// 读取配置文件中配置,转换成DocumentString name = "applicationConfig: [" + location + "]";List<Document> documents = loadDocuments(loader, name, resource);if (CollectionUtils.isEmpty(documents)) {if (this.logger.isTraceEnabled()) {StringBuilder description = getDescription("Skipped unloaded config ", location, resource,profile);this.logger.trace(description);}return;}List<Document> loaded = new ArrayList<>();// 添加新的active和include的profilefor (Document document : documents) {if (filter.match(document)) {addActiveProfiles(document.getActiveProfiles());addIncludedProfiles(document.getIncludeProfiles());loaded.add(document);}}// 将此次加载的Document顺序颠倒Collections.reverse(loaded);if (!loaded.isEmpty()) {loaded.forEach((document) -> consumer.accept(profile, document));if (this.logger.isDebugEnabled()) {StringBuilder description = getDescription("Loaded config file ", location, resource, profile);this.logger.debug(description);}}}catch (Exception ex) {throw new IllegalStateException("Failed to load property source from location '" + location + "'", ex);}
}
  1. 通过一系列的校验,比如文件资源、文件扩展名是否存在。当校验都通过了,才会去加载资源。
  2. loadDocuments方法读取配置文件中的信息,封装成了Document返回。虽然返回的是List,实际上List中只有一个元素,因为每次只会加载一个资源文件。可能是Spring为了扩展,而返回List吧。
java">private List<Document> loadDocuments(PropertySourceLoader loader, String name, Resource resource)throws IOException {DocumentsCacheKey cacheKey = new DocumentsCacheKey(loader, resource);List<Document> documents = this.loadDocumentsCache.get(cacheKey);if (documents == null) {List<PropertySource<?>> loaded = loader.load(name, resource);documents = asDocuments(loaded);this.loadDocumentsCache.put(cacheKey, documents);}return documents;
}

加载资源文件,根据文件的扩展名,回调了对应的PropertiesPropertySourceLoader#load、YamlPropertySourceLoader#load。

  1. resourceLoader.getResource获取文件资源支持通过URL获取。DefaultResourceLoader#getResource。
java">@Override
public Resource getResource(String location) {Assert.notNull(location, "Location must not be null");for (ProtocolResolver protocolResolver : getProtocolResolvers()) {Resource resource = protocolResolver.resolve(location, this);if (resource != null) {return resource;}}if (location.startsWith("/")) {return getResourceByPath(location);}else if (location.startsWith(CLASSPATH_URL_PREFIX)) {return new ClassPathResource(location.substring(CLASSPATH_URL_PREFIX.length()), getClassLoader());}else {try {// Try to parse the location as a URL...URL url = new URL(location);return (ResourceUtils.isFileURL(url) ? new FileUrlResource(url) : new UrlResource(url));}catch (MalformedURLException ex) {// No URL -> resolve as resource path.return getResourceByPath(location);}}
}
  1. asDocuments,将加载到的资源封装成Document
java">private List<Document> asDocuments(List<PropertySource<?>> loaded) {if (loaded == null) {return Collections.emptyList();}return loaded.stream().map((propertySource) -> {Binder binder = new Binder(ConfigurationPropertySources.from(propertySource),this.placeholdersResolver);return new Document(propertySource, binder.bind("spring.profiles", STRING_ARRAY).orElse(null),getProfiles(binder, ACTIVE_PROFILES_PROPERTY), getProfiles(binder, INCLUDE_PROFILES_PROPERTY));}).collect(Collectors.toList());
}

通过binder将propertySource中的spring.profiles.active和spring.profiles.include解析成String数组,分别绑定到了Document对象的activeProfiles和includeProfiles属性,以便后面使用。
6. 遍历所有的Document对象,实际上只有一个Document,因为文件资源只有一个。将Document中的activeProfiles和includeProfiles重新加入到profiles中。因为最外面第一层Loader#load()中正在遍历profiles,下次循环会重新加载后续的profile。
7. loaded.forEach((document) -> consumer.accept(profile, document));

配置文件加载顺序总结

  1. 遍历profile。假如启动脚本中有spring.profiles.active、spring.profiles.include的profile为dev,则遍历null、dev。现进先出,先加载为null的profile。越先加载的profile,优先级越低
  2. 遍历location。相关配置:spring.config.location、spring.config.additional-location,默认配置为classpath:/,classpath:/config/,file:./,file:./config/。注意:会先将location的顺序颠倒,再去加载。
  3. 遍历文件名称。相关配置spring.config.name。默认application。
  4. 判断文件名称是否为空。如果location不是文件夹(不以“/”结尾,那么就认为不是文件夹),则使用location去加载文件。否则就拼接文件名称加载。
  5. 遍历文件扩展名。先遍历资源加载器,每一个资源加载器都支持不同的文件扩展名。PropertiesPropertySourceLoader支持properties、xml,YamlPropertySourceLoader支持yml、yaml。
  6. 最终将加载到的所有信息放入到ConfigFileApplicationListener.Loader#loaded。loaded是LinkedHashMap,key为profile,value为对应的文件名称加载是所有资源propertySource。最终会颠倒profile加载的顺序,将propertySource放入到environment中。
  7. 放入environment的先后顺序决定了取配置的优先级,越先加入到environment中的propertySource,优先级越高。

问题:为什么先加入到environment中的propertySource,优先级越高?

environment#getProperty()获取属性key所对应的值。调用链路如下

AbstractEnvironment#getProperty(java.lang.String)

java">public String getProperty(String key) {return this.propertyResolver.getProperty(key);
}

org.springframework.core.env.PropertySourcesPropertyResolver#getProperty(java.lang.String)

java">public String getProperty(String key) {return getProperty(key, String.class, true);
}

PropertySourcesPropertyResolver#getProperty(java.lang.String, java.lang.Class, boolean)

java">protected <T> T getProperty(String key, Class<T> targetValueType, boolean resolveNestedPlaceholders) {if (this.propertySources != null) {for (PropertySource<?> propertySource : this.propertySources) {if (logger.isTraceEnabled()) {logger.trace("Searching for key '" + key + "' in PropertySource '" +propertySource.getName() + "'");}Object value = propertySource.getProperty(key);if (value != null) {if (resolveNestedPlaceholders && value instanceof String) {value = resolveNestedPlaceholders((String) value);}logKeyFound(key, propertySource, value);return convertValueIfNecessary(value, targetValueType);}}}if (logger.isTraceEnabled()) {logger.trace("Could not find key '" + key + "' in any property source");}return null;
}

我们可以很清晰的看到,遍历了propertySources,从propertySource取到不为null的值。解析占位符、转换值类型之后,就返回了。

遗留问题:

  1. bootstrap.yml加载逻辑BootstrapApplicationListener。
  2. nacos中的配置文件如何加载到的?getSearchLocations中使用URL协议吗?nacos源码研究。

http://www.ppmy.cn/server/38966.html

相关文章

索引失效情况

&#x1f4dd;个人主页&#xff1a;五敷有你 &#x1f525;系列专栏&#xff1a;面经 ⛺️稳中求进&#xff0c;晒太阳 一、索引列上运算操作。 不要在索引列上进行运算操作&#xff0c;否则索引会失效。 在tb_user的phone列加上索引&#xff0c;然后进行条件查询&am…

去除视频背景音乐或人物声音的4种方法,建议收藏

你是否曾想移除视频中令人分心的声音呢&#xff1f;对于需要裁剪声音或去除背景噪音的视频来说&#xff0c;消音是一种非常有用的技能。那么&#xff0c;视频怎么消除声音&#xff1f;看看下文就知道了。 方法一&#xff1a;使用 智优影 去除视频中的音频 在线转换工具不仅支持…

【Java】还不会数组?一文万字全搞定

前言&#xff1a;前面两章我们详细讲解了Java基本程序设计结构中的基本知识&#xff0c;&#xff0c;包括&#xff1a;一个简单的Java应用&#xff0c;注释&#xff0c;数据类型&#xff0c;变量与常量&#xff0c;运算符&#xff0c;字符串&#xff0c;输入输出&#xff0c;控…

Yarn:下一代JavaScript包管理器的安装与实战指南

当然&#xff0c;让我们深入探讨Yarn——一个高效、可靠的JavaScript包管理器&#xff0c;它为前端开发带来了新的速度和便利。Yarn由Facebook、Google、Exponent和Tilde公司共同推出&#xff0c;旨在解决npm&#xff08;Node.js包管理器&#xff09;存在的问题&#xff0c;如依…

ELK+kafka日志采集

ElasticSeach&#xff08;存储日志信息&#xff09; Logstash&#xff08;搬运工&#xff09; Kibana 连接ElasticSeach图形化界面查询日志 ELK采集日志的原理&#xff1a; 在每个服务器上安装LogstashLogstash需要配置固定读取某个日志文件Logstash将日志文件格式化为json的…

汇编--栈和寄存器

栈 栈是一种运算受限的线性表&#xff0c;其限定仅在表尾进行插入和删除操作的线性表&#xff0c;表尾也被叫做栈顶。简单概括就是我们对于元素的操作只能够在栈顶进行&#xff0c;也造就了其先进后出的结构特性。 栈 这种内存空间其实本质上有两种操作&#xff1a;将数据放入…

Python sqlite3库 实现 数据库基础及应用 输入地点,可输出该地点的爱国主义教育基地名称和批次的查询结果。

目录 【第11次课】实验十数据库基础及应用1-查询 要求: 提示: 运行结果&#xff1a; 【第11次课】实验十数据库基础及应用1-查询 声明&#xff1a;著作权归作者所有。商业转载请联系作者获得授权&#xff0c;非商业转载请注明出处。 1.简答题 数据库文件Edu_Base.db&#…