From e64a145ef0b0ac3a3bf97c41cff67af519ee6a71 Mon Sep 17 00:00:00 2001 From: Madhura Bhave Date: Fri, 31 Jan 2020 18:03:55 -0800 Subject: [PATCH] Add support for wildcard locations for properties and YAML files Closes gh-19909 --- .../docs/asciidoc/spring-boot-features.adoc | 8 +- .../config/ConfigFileApplicationListener.java | 98 +++++++++++-------- .../ConfigFileApplicationListenerTests.java | 32 +++++- .../config/a/testproperties.properties | 1 + .../config/b/testproperties.properties | 1 + 5 files changed, 98 insertions(+), 42 deletions(-) create mode 100644 spring-boot-project/spring-boot/src/test/resources/config/a/testproperties.properties create mode 100644 spring-boot-project/spring-boot/src/test/resources/config/b/testproperties.properties diff --git a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/spring-boot-features.adoc b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/spring-boot-features.adoc index 845a77b1a5..1e90297dc5 100644 --- a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/spring-boot-features.adoc +++ b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/spring-boot-features.adoc @@ -396,6 +396,10 @@ On your application classpath (for example, inside your jar) you can have an `ap When running in a new environment, an `application.properties` file can be provided outside of your jar that overrides the `name`. For one-off testing, you can launch with a specific command line switch (for example, `java -jar app.jar --name="Spring"`). +NOTE: Spring Boot also supports wildcard locations when loading configuration files. +By default, a wildcard location of `config/*/` outside of your jar is supported. +Wildcard locations are also supported when specifying `spring.config.additional-location` and `spring.config.location`. + [[boot-features-external-config-application-json]] [TIP] ==== @@ -492,10 +496,11 @@ If `spring.config.location` contains directories (as opposed to files), they sho Files specified in `spring.config.location` are used as-is, with no support for profile-specific variants, and are overridden by any profile-specific properties. Config locations are searched in reverse order. -By default, the configured locations are `classpath:/,classpath:/config/,file:./,file:./config/`. +By default, the configured locations are `classpath:/,classpath:/config/,file:./,file:./config/*/,file:./config/`. The resulting search order is the following: . `file:./config/` +. `file:./config/*/` . `file:./` . `classpath:/config/` . `classpath:/` @@ -513,6 +518,7 @@ For example, if additional locations of `classpath:/custom-config/,file:./custom . `file:./custom-config/` . `classpath:custom-config/` . `file:./config/` +. `file:./config/*/` . `file:./` . `classpath:/config/` . `classpath:/` diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigFileApplicationListener.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigFileApplicationListener.java index b33937b87a..897b3cc61e 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigFileApplicationListener.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigFileApplicationListener.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -62,6 +62,7 @@ import org.springframework.core.env.PropertySource; import org.springframework.core.io.DefaultResourceLoader; import org.springframework.core.io.Resource; import org.springframework.core.io.ResourceLoader; +import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.core.io.support.SpringFactoriesLoader; import org.springframework.util.Assert; import org.springframework.util.CollectionUtils; @@ -75,6 +76,7 @@ import org.springframework.util.StringUtils; * 'application.properties' and/or 'application.yml' files in the following locations: *