Browse Source

Add StandardConfigDataResource.getProfile method

Add a `StandardConfigDataResource.getProfile()` method so that it's
possible to tell the profile used when reading a profile specific
resource.

Fixes gh-25940
pull/26435/head
Phillip Webb 5 years ago
parent
commit
302d500ee9
  1. 4
      spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/StandardConfigDataReference.java
  2. 9
      spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/StandardConfigDataResource.java
  3. 7
      spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataEnvironmentPostProcessorTests.java

4
spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/StandardConfigDataReference.java

@ -74,6 +74,10 @@ class StandardConfigDataReference { @@ -74,6 +74,10 @@ class StandardConfigDataReference {
return this.directory;
}
String getProfile() {
return this.profile;
}
boolean isSkippable() {
return this.configDataLocation.isOptional() || this.directory != null || this.profile != null;
}

9
spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/StandardConfigDataResource.java

@ -74,6 +74,15 @@ public class StandardConfigDataResource extends ConfigDataResource { @@ -74,6 +74,15 @@ public class StandardConfigDataResource extends ConfigDataResource {
return this.resource;
}
/**
* Return the profile or {@code null} if the resource is not profile specific.
* @return the profile or {@code null}
* @since 2.4.6
*/
public String getProfile() {
return this.reference.getProfile();
}
boolean isEmptyDirectory() {
return this.emptyDirectory;
}

7
spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataEnvironmentPostProcessorTests.java

@ -127,6 +127,13 @@ class ConfigDataEnvironmentPostProcessorTests { @@ -127,6 +127,13 @@ class ConfigDataEnvironmentPostProcessorTests {
assertThat(this.environment.getActiveProfiles()).containsExactly("dev");
assertThat(listener.getAddedPropertySources()).hasSizeGreaterThan(0);
assertThat(listener.getProfiles().getActive()).containsExactly("dev");
assertThat(listener.getAddedPropertySources().stream().anyMatch((added) -> hasDevProfile(added.getResource())))
.isTrue();
}
private boolean hasDevProfile(ConfigDataResource resource) {
return (resource instanceof StandardConfigDataResource)
&& "dev".equals(((StandardConfigDataResource) resource).getProfile());
}
}

Loading…
Cancel
Save