From b5c92d51744034175a81287a45944a71c6615745 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Wed, 16 Jun 2021 22:30:29 -0700 Subject: [PATCH] Allow indirect standard profile-specific imports Effectively revert commit d1b256a16936 so that profile-specific imports can again be used with or without a parent import. Fixes gh-26858 --- .../src/docs/asciidoc/spring-boot-features.adoc | 9 ++++++++- .../config/StandardConfigDataLocationResolver.java | 3 --- ...nfigDataEnvironmentPostProcessorIntegrationTests.java | 4 ++-- 3 files changed, 10 insertions(+), 6 deletions(-) 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 c96e25b299..59c63dd6f9 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 @@ -642,7 +642,10 @@ They must be defined as an environment property (typically an OS environment var If `spring.config.location` contains directories (as opposed to files), they should end in `/`. At runtime they will be appended with the names generated from `spring.config.name` before being loaded. -Files specified in `spring.config.location` are used as-is. +Files specified in `spring.config.location` are imported directly. + +NOTE: Both directory and file location values are also expanded to check for <>. +For example, if you have a `spring.config.location` of `classpath:myconfig.properties`, you will also find appropriate `classpath:myconfig-.properties` files are loaded. In most situations, each configprop:spring.config.location[] item you add will reference a single file or directory. Locations are processed in the order that they are defined and later ones can override the values of earlier ones. @@ -782,6 +785,7 @@ For example, you might have the following in your classpath `application.propert This will trigger the import of a `dev.properties` file in current directory (if such a file exists). Values from the imported `dev.properties` will take precedence over the file that triggered the import. In the above example, the `dev.properties` could redefine `spring.application.name` to a different value. + An import will only be imported once no matter how many times it is declared. The order an import is defined inside a single document within the properties/yaml file doesn't matter. For instance, the two examples below produce the same result: @@ -809,6 +813,9 @@ In both of the above examples, the values from the `my.properties` file will tak Several locations can be specified under a single `spring.config.import` key. Locations will be processed in the order that they are defined, with later imports taking precedence. +NOTE: When appropriate, <> are also considered for import. +The example above would import both `my.properties` as well as any `my-.properties` variants. + [TIP] ==== Spring Boot includes pluggable API that allows various different location addresses to be supported. diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/StandardConfigDataLocationResolver.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/StandardConfigDataLocationResolver.java index 5cf29b3dea..5cba39ba03 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/StandardConfigDataLocationResolver.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/StandardConfigDataLocationResolver.java @@ -144,9 +144,6 @@ public class StandardConfigDataLocationResolver @Override public List resolveProfileSpecific(ConfigDataLocationResolverContext context, ConfigDataLocation location, Profiles profiles) { - if (context.getParent() != null) { - return null; - } return resolve(getProfileSpecificReferences(context, location.split(), profiles)); } diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataEnvironmentPostProcessorIntegrationTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataEnvironmentPostProcessorIntegrationTests.java index 3a5a969f12..fb0b2c9b80 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataEnvironmentPostProcessorIntegrationTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataEnvironmentPostProcessorIntegrationTests.java @@ -611,13 +611,13 @@ class ConfigDataEnvironmentPostProcessorIntegrationTests { assertThat(context.getEnvironment().getProperty("my.value")).isEqualTo("iwasimported"); } - @Test + @Test // gh-26858 void runWhenImportWithProfileVariantOrdersPropertySourcesCorrectly() { this.application.setAdditionalProfiles("dev"); ConfigurableApplicationContext context = this.application .run("--spring.config.location=classpath:application-import-with-profile-variant.properties"); assertThat(context.getEnvironment().getProperty("my.value")) - .isEqualTo("application-import-with-profile-variant-dev"); + .isEqualTo("application-import-with-profile-variant-imported-dev"); } @Test