Skip to content

Commit da3ff29

Browse files
philwebbsnicoll
authored andcommitted
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
1 parent 9c6b1b6 commit da3ff29

File tree

2 files changed

+52
-9
lines changed

2 files changed

+52
-9
lines changed

spring-core/src/main/java/org/springframework/core/env/AbstractEnvironment.java

+26-9
Original file line numberDiff line numberDiff line change
@@ -227,15 +227,15 @@ public String[] getActiveProfiles() {
227227
/**
228228
* Return the set of active profiles as explicitly set through
229229
* {@link #setActiveProfiles} or if the current set of active profiles
230-
* is empty, check for the presence of the {@value #ACTIVE_PROFILES_PROPERTY_NAME}
231-
* property and assign its value to the set of active profiles.
230+
* is empty, check for the presence of {@link #doGetActiveProfilesProperty()}
231+
* and assign its value to the set of active profiles.
232232
* @see #getActiveProfiles()
233-
* @see #ACTIVE_PROFILES_PROPERTY_NAME
233+
* @see #doGetActiveProfilesProperty()
234234
*/
235235
protected Set<String> doGetActiveProfiles() {
236236
synchronized (this.activeProfiles) {
237237
if (this.activeProfiles.isEmpty()) {
238-
String profiles = getProperty(ACTIVE_PROFILES_PROPERTY_NAME);
238+
String profiles = doGetActiveProfilesProperty();
239239
if (StringUtils.hasText(profiles)) {
240240
setActiveProfiles(StringUtils.commaDelimitedListToStringArray(
241241
StringUtils.trimAllWhitespace(profiles)));
@@ -245,6 +245,15 @@ protected Set<String> doGetActiveProfiles() {
245245
}
246246
}
247247

248+
/**
249+
* Return the property value for the active profiles.
250+
* @see #ACTIVE_PROFILES_PROPERTY_NAME
251+
* @since 5.3.4
252+
*/
253+
protected String doGetActiveProfilesProperty() {
254+
return getProperty(ACTIVE_PROFILES_PROPERTY_NAME);
255+
}
256+
248257
@Override
249258
public void setActiveProfiles(String... profiles) {
250259
Assert.notNull(profiles, "Profile array must not be null");
@@ -282,18 +291,17 @@ public String[] getDefaultProfiles() {
282291
* Return the set of default profiles explicitly set via
283292
* {@link #setDefaultProfiles(String...)} or if the current set of default profiles
284293
* consists only of {@linkplain #getReservedDefaultProfiles() reserved default
285-
* profiles}, then check for the presence of the
286-
* {@value #DEFAULT_PROFILES_PROPERTY_NAME} property and assign its value (if any)
287-
* to the set of default profiles.
294+
* profiles}, then check for the presence of {@link #doGetActiveProfilesProperty()}
295+
* and assign its value (if any) to the set of default profiles.
288296
* @see #AbstractEnvironment()
289297
* @see #getDefaultProfiles()
290-
* @see #DEFAULT_PROFILES_PROPERTY_NAME
291298
* @see #getReservedDefaultProfiles()
299+
* @see #doGetDefaultProfilesProperty()
292300
*/
293301
protected Set<String> doGetDefaultProfiles() {
294302
synchronized (this.defaultProfiles) {
295303
if (this.defaultProfiles.equals(getReservedDefaultProfiles())) {
296-
String profiles = getProperty(DEFAULT_PROFILES_PROPERTY_NAME);
304+
String profiles = doGetDefaultProfilesProperty();
297305
if (StringUtils.hasText(profiles)) {
298306
setDefaultProfiles(StringUtils.commaDelimitedListToStringArray(
299307
StringUtils.trimAllWhitespace(profiles)));
@@ -303,6 +311,15 @@ protected Set<String> doGetDefaultProfiles() {
303311
}
304312
}
305313

314+
/**
315+
* Return the property value for the default profiles.
316+
* @see #DEFAULT_PROFILES_PROPERTY_NAME
317+
* @since 5.3.4
318+
*/
319+
protected String doGetDefaultProfilesProperty() {
320+
return getProperty(DEFAULT_PROFILES_PROPERTY_NAME);
321+
}
322+
306323
/**
307324
* Specify the set of profiles to be made active by default if no other profiles
308325
* are explicitly made active through {@link #setActiveProfiles}.

spring-core/src/test/java/org/springframework/core/env/CustomEnvironmentTests.java

+26
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
import java.util.Collections;
2020
import java.util.HashSet;
21+
import java.util.LinkedHashMap;
22+
import java.util.Map;
2123
import java.util.Set;
2224

2325
import org.junit.jupiter.api.Test;
@@ -105,6 +107,30 @@ protected Set<String> getReservedDefaultProfiles() {
105107
assertThat(env.acceptsProfiles(Profiles.of("a1 | a2"))).isFalse();
106108
}
107109

110+
@Test
111+
public void withNoProfileProperties() {
112+
ConfigurableEnvironment env = new AbstractEnvironment() {
113+
114+
@Override
115+
protected String doGetActiveProfilesProperty() {
116+
return null;
117+
}
118+
119+
@Override
120+
protected String doGetDefaultProfilesProperty() {
121+
return null;
122+
}
123+
124+
};
125+
Map<String, Object> values = new LinkedHashMap<>();
126+
values.put(AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME, "a,b,c");
127+
values.put(AbstractEnvironment.DEFAULT_PROFILES_PROPERTY_NAME, "d,e,f");
128+
PropertySource<?> propertySource = new MapPropertySource("test", values);
129+
env.getPropertySources().addFirst(propertySource);
130+
assertThat(env.getActiveProfiles()).isEmpty();
131+
assertThat(env.getDefaultProfiles()).containsExactly(AbstractEnvironment.RESERVED_DEFAULT_PROFILE_NAME);
132+
}
133+
108134
private Profiles defaultProfile() {
109135
return Profiles.of(AbstractEnvironment.RESERVED_DEFAULT_PROFILE_NAME);
110136
}

0 commit comments

Comments
 (0)