From 6ce75425b1e2897304113cf7ad615b24d5b2db21 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Wed, 27 Jan 2021 15:21:30 -0800 Subject: [PATCH] Allow bypass of active/default properties Introduce protected methods that can be used to bypass or change the way that active and default property values are read. See gh-26461 --- .../core/env/AbstractEnvironment.java | 35 ++++++++++++++----- .../core/env/CustomEnvironmentTests.java | 26 ++++++++++++++ 2 files changed, 52 insertions(+), 9 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/core/env/AbstractEnvironment.java b/spring-core/src/main/java/org/springframework/core/env/AbstractEnvironment.java index 56c894fa0501..8568f194d77f 100644 --- a/spring-core/src/main/java/org/springframework/core/env/AbstractEnvironment.java +++ b/spring-core/src/main/java/org/springframework/core/env/AbstractEnvironment.java @@ -227,15 +227,15 @@ public String[] getActiveProfiles() { /** * Return the set of active profiles as explicitly set through * {@link #setActiveProfiles} or if the current set of active profiles - * is empty, check for the presence of the {@value #ACTIVE_PROFILES_PROPERTY_NAME} - * property and assign its value to the set of active profiles. + * is empty, check for the presence of {@link #doGetActiveProfilesProperty()} + * and assign its value to the set of active profiles. * @see #getActiveProfiles() - * @see #ACTIVE_PROFILES_PROPERTY_NAME + * @see #doGetActiveProfilesProperty() */ protected Set doGetActiveProfiles() { synchronized (this.activeProfiles) { if (this.activeProfiles.isEmpty()) { - String profiles = getProperty(ACTIVE_PROFILES_PROPERTY_NAME); + String profiles = doGetActiveProfilesProperty(); if (StringUtils.hasText(profiles)) { setActiveProfiles(StringUtils.commaDelimitedListToStringArray( StringUtils.trimAllWhitespace(profiles))); @@ -245,6 +245,15 @@ protected Set doGetActiveProfiles() { } } + /** + * Return the property value for the active profiles. + * @see #ACTIVE_PROFILES_PROPERTY_NAME + * @since 5.3.4 + */ + protected String doGetActiveProfilesProperty() { + return getProperty(ACTIVE_PROFILES_PROPERTY_NAME); + } + @Override public void setActiveProfiles(String... profiles) { Assert.notNull(profiles, "Profile array must not be null"); @@ -282,18 +291,17 @@ public String[] getDefaultProfiles() { * Return the set of default profiles explicitly set via * {@link #setDefaultProfiles(String...)} or if the current set of default profiles * consists only of {@linkplain #getReservedDefaultProfiles() reserved default - * profiles}, then check for the presence of the - * {@value #DEFAULT_PROFILES_PROPERTY_NAME} property and assign its value (if any) - * to the set of default profiles. + * profiles}, then check for the presence of {@link #doGetActiveProfilesProperty()} + * and assign its value (if any) to the set of default profiles. * @see #AbstractEnvironment() * @see #getDefaultProfiles() - * @see #DEFAULT_PROFILES_PROPERTY_NAME * @see #getReservedDefaultProfiles() + * @see #doGetDefaultProfilesProperty() */ protected Set doGetDefaultProfiles() { synchronized (this.defaultProfiles) { if (this.defaultProfiles.equals(getReservedDefaultProfiles())) { - String profiles = getProperty(DEFAULT_PROFILES_PROPERTY_NAME); + String profiles = doGetDefaultProfilesProperty(); if (StringUtils.hasText(profiles)) { setDefaultProfiles(StringUtils.commaDelimitedListToStringArray( StringUtils.trimAllWhitespace(profiles))); @@ -303,6 +311,15 @@ protected Set doGetDefaultProfiles() { } } + /** + * Return the property value for the default profiles. + * @see #DEFAULT_PROFILES_PROPERTY_NAME + * @since 5.3.4 + */ + protected String doGetDefaultProfilesProperty() { + return getProperty(DEFAULT_PROFILES_PROPERTY_NAME); + } + /** * Specify the set of profiles to be made active by default if no other profiles * are explicitly made active through {@link #setActiveProfiles}. diff --git a/spring-core/src/test/java/org/springframework/core/env/CustomEnvironmentTests.java b/spring-core/src/test/java/org/springframework/core/env/CustomEnvironmentTests.java index f564c74a6073..4cb4a87bc791 100644 --- a/spring-core/src/test/java/org/springframework/core/env/CustomEnvironmentTests.java +++ b/spring-core/src/test/java/org/springframework/core/env/CustomEnvironmentTests.java @@ -18,6 +18,8 @@ import java.util.Collections; import java.util.HashSet; +import java.util.LinkedHashMap; +import java.util.Map; import java.util.Set; import org.junit.jupiter.api.Test; @@ -105,6 +107,30 @@ protected Set getReservedDefaultProfiles() { assertThat(env.acceptsProfiles(Profiles.of("a1 | a2"))).isFalse(); } + @Test + public void withNoProfileProperties() { + ConfigurableEnvironment env = new AbstractEnvironment() { + + @Override + protected String doGetActiveProfilesProperty() { + return null; + } + + @Override + protected String doGetDefaultProfilesProperty() { + return null; + } + + }; + Map values = new LinkedHashMap<>(); + values.put(AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME, "a,b,c"); + values.put(AbstractEnvironment.DEFAULT_PROFILES_PROPERTY_NAME, "d,e,f"); + PropertySource propertySource = new MapPropertySource("test", values); + env.getPropertySources().addFirst(propertySource); + assertThat(env.getActiveProfiles()).isEmpty(); + assertThat(env.getDefaultProfiles()).containsExactly(AbstractEnvironment.RESERVED_DEFAULT_PROFILE_NAME); + } + private Profiles defaultProfile() { return Profiles.of(AbstractEnvironment.RESERVED_DEFAULT_PROFILE_NAME); }