From 5774ea3f0c81de9776fc06cfbcb7c7d055f26333 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Thu, 25 Mar 2021 13:53:03 -0700 Subject: [PATCH] Support profile specific ConfigData imports Update the `ConfigData` import support to allow individual property sources to be imported with a higher precedence than profile specific imports. Prior to this commit, imported sources would always have a higher precedence than the file that imported them, but a lower precedence than any profile-specific variant of the same file. For example, given an `application.properties` that imports `myconfig`, the contributor tree would be as follows: ROOT +- `application.properties` | +- myconfig +- `application-.properties` The precedence would be: 1) `application-.properties` 2) myconfig 3) `application.properties` This works well for most situations, but can be confusing if import is for a profile-specific property source. For example: ROOT +- `application.properties` | +- myconfig | +- myconfig- +- `application-.properties` Results in the order precedence of: 1) `application-.properties` 2) myconfig- 3) myconfig 4) `application.properties` This means that whilst `myconfig` overrides `application.properties`, `myconfig-profile` does not override `application-.properties`. For this specific situation, the preferable order would be: 1) myconfig- 2) `application-.properties` 3) myconfig 4) `application.properties` To support this alternative ordering a new `PROFILE_SPECIFIC` config data option has been added. Additionally, options may now be specified on a per-source basis by using the `PropertySourceOptions` interface. Fixes gh-25766 --- .../boot/context/config/ConfigData.java | 189 +++++++++++++++++- .../context/config/ConfigDataEnvironment.java | 5 +- .../ConfigDataEnvironmentContributor.java | 137 ++++++++++--- .../InvalidConfigDataPropertyException.java | 3 +- ...ConfigDataEnvironmentContributorTests.java | 16 ++ ...edWithProfileSpecificIntegrationTests.java | 132 ++++++++++++ .../boot/context/config/ConfigDataTests.java | 74 ++++++- ...validConfigDataPropertyExceptionTests.java | 5 +- .../test/resources/META-INF/spring.factories | 6 +- ...gimportwithprofilespecific-prod.properties | 1 + ...configimportwithprofilespecific.properties | 2 + 11 files changed, 523 insertions(+), 47 deletions(-) create mode 100644 spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataEnvironmentPostProcessorImportCombinedWithProfileSpecificIntegrationTests.java create mode 100644 spring-boot-project/spring-boot/src/test/resources/configimportwithprofilespecific-prod.properties create mode 100644 spring-boot-project/spring-boot/src/test/resources/configimportwithprofilespecific.properties diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigData.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigData.java index 5a1431d21a..fcb5eb83fc 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigData.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigData.java @@ -43,7 +43,7 @@ public final class ConfigData { private final List> propertySources; - private final Set