Skip to content

Commit d4c609f

Browse files
philwebbsnicoll
authored andcommitted
Allow customer resolver and property sources
Add factory methods to `AbstractEnvironment` that allow a custom `ConfigurablePropertyResolver` and `MutablePropertySources` instance to be used. See gh-26462
1 parent 3524401 commit d4c609f

File tree

4 files changed

+95
-4
lines changed

4 files changed

+95
-4
lines changed

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

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,9 @@ public abstract class AbstractEnvironment implements ConfigurableEnvironment {
107107

108108
private final Set<String> defaultProfiles = new LinkedHashSet<>(getReservedDefaultProfiles());
109109

110-
private final MutablePropertySources propertySources = new MutablePropertySources();
110+
private final MutablePropertySources propertySources;
111111

112-
private final ConfigurablePropertyResolver propertyResolver =
113-
new PropertySourcesPropertyResolver(this.propertySources);
112+
private final ConfigurablePropertyResolver propertyResolver;
114113

115114

116115
/**
@@ -121,9 +120,44 @@ public abstract class AbstractEnvironment implements ConfigurableEnvironment {
121120
* @see #customizePropertySources(MutablePropertySources)
122121
*/
123122
public AbstractEnvironment() {
124-
customizePropertySources(this.propertySources);
123+
this(new MutablePropertySources());
125124
}
126125

126+
/**
127+
* Create a new {@code Environment} instance with a specific
128+
* {@link MutablePropertySources} instance, calling back to
129+
* {@link #customizePropertySources(MutablePropertySources)} during
130+
* construction to allow subclasses to contribute or manipulate
131+
* {@link PropertySource} instances as appropriate.
132+
* @see #customizePropertySources(MutablePropertySources)
133+
* @since 5.3.4
134+
*/
135+
protected AbstractEnvironment(MutablePropertySources propertySources) {
136+
this.propertySources = propertySources;
137+
this.propertyResolver = createPropertyResolver(propertySources);
138+
customizePropertySources(propertySources);
139+
}
140+
141+
142+
/**
143+
* Factory method used to create the {@link ConfigurablePropertyResolver}
144+
* instance used by the Environment.
145+
* @see #getPropertyResolver()
146+
* @since 5.3.4
147+
*/
148+
protected ConfigurablePropertyResolver createPropertyResolver(
149+
MutablePropertySources propertySources) {
150+
return new PropertySourcesPropertyResolver(propertySources);
151+
}
152+
153+
/**
154+
* Return the {@link ConfigurablePropertyResolver} being used by the
155+
* {@link Environment}.
156+
* @see #createPropertyResolver(MutablePropertySources)
157+
*/
158+
protected final ConfigurablePropertyResolver getPropertyResolver() {
159+
return this.propertyResolver;
160+
}
127161

128162
/**
129163
* Customize the set of {@link PropertySource} objects to be searched by this

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,14 @@ public class StandardEnvironment extends AbstractEnvironment {
6060
public static final String SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME = "systemProperties";
6161

6262

63+
public StandardEnvironment() {
64+
}
65+
66+
protected StandardEnvironment(MutablePropertySources propertySources) {
67+
super(propertySources);
68+
}
69+
70+
6371
/**
6472
* Customize the set of property sources with those appropriate for any standard
6573
* Java environment:

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,47 @@ protected String doGetDefaultProfilesProperty() {
129129
assertThat(env.getDefaultProfiles()).containsExactly(AbstractEnvironment.RESERVED_DEFAULT_PROFILE_NAME);
130130
}
131131

132+
@Test
133+
public void withCustomMutablePropertySources() {
134+
class CustomMutablePropertySources extends MutablePropertySources {
135+
}
136+
MutablePropertySources propertySources = new CustomMutablePropertySources();
137+
ConfigurableEnvironment env = new AbstractEnvironment(propertySources) {
138+
};
139+
assertThat(env.getPropertySources()).isInstanceOf(CustomMutablePropertySources.class);
140+
}
141+
142+
@Test
143+
void withCustomPropertyResolver() {
144+
class CustomPropertySourcesPropertyResolver extends PropertySourcesPropertyResolver {
145+
146+
public CustomPropertySourcesPropertyResolver(
147+
PropertySources propertySources) {
148+
super(propertySources);
149+
}
150+
151+
@Override
152+
public String getProperty(String key) {
153+
return super.getProperty(key)+"-test";
154+
}
155+
156+
}
157+
ConfigurableEnvironment env = new AbstractEnvironment() {
158+
159+
@Override
160+
protected ConfigurablePropertyResolver createPropertyResolver(
161+
MutablePropertySources propertySources) {
162+
return new CustomPropertySourcesPropertyResolver(propertySources);
163+
}
164+
165+
};
166+
Map<String, Object> values = new LinkedHashMap<>();
167+
values.put("spring", "framework");
168+
PropertySource<?> propertySource = new MapPropertySource("test", values);
169+
env.getPropertySources().addFirst(propertySource);
170+
assertThat(env.getProperty("spring")).isEqualTo("framework-test");
171+
}
172+
132173
private Profiles defaultProfile() {
133174
return Profiles.of(AbstractEnvironment.RESERVED_DEFAULT_PROFILE_NAME);
134175
}

spring-web/src/main/java/org/springframework/web/context/support/StandardServletEnvironment.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,14 @@ public class StandardServletEnvironment extends StandardEnvironment implements C
5454
public static final String JNDI_PROPERTY_SOURCE_NAME = "jndiProperties";
5555

5656

57+
public StandardServletEnvironment() {
58+
}
59+
60+
protected StandardServletEnvironment(MutablePropertySources propertySources) {
61+
super(propertySources);
62+
}
63+
64+
5765
/**
5866
* Customize the set of property sources with those contributed by superclasses as
5967
* well as those appropriate for standard servlet-based environments:

0 commit comments

Comments
 (0)