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
This commit is contained in:
committed by
Stephane Nicoll
parent
3524401bf1
commit
d4c609f2f0
@@ -129,6 +129,47 @@ class CustomEnvironmentTests {
|
||||
assertThat(env.getDefaultProfiles()).containsExactly(AbstractEnvironment.RESERVED_DEFAULT_PROFILE_NAME);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void withCustomMutablePropertySources() {
|
||||
class CustomMutablePropertySources extends MutablePropertySources {
|
||||
}
|
||||
MutablePropertySources propertySources = new CustomMutablePropertySources();
|
||||
ConfigurableEnvironment env = new AbstractEnvironment(propertySources) {
|
||||
};
|
||||
assertThat(env.getPropertySources()).isInstanceOf(CustomMutablePropertySources.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
void withCustomPropertyResolver() {
|
||||
class CustomPropertySourcesPropertyResolver extends PropertySourcesPropertyResolver {
|
||||
|
||||
public CustomPropertySourcesPropertyResolver(
|
||||
PropertySources propertySources) {
|
||||
super(propertySources);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getProperty(String key) {
|
||||
return super.getProperty(key)+"-test";
|
||||
}
|
||||
|
||||
}
|
||||
ConfigurableEnvironment env = new AbstractEnvironment() {
|
||||
|
||||
@Override
|
||||
protected ConfigurablePropertyResolver createPropertyResolver(
|
||||
MutablePropertySources propertySources) {
|
||||
return new CustomPropertySourcesPropertyResolver(propertySources);
|
||||
}
|
||||
|
||||
};
|
||||
Map<String, Object> values = new LinkedHashMap<>();
|
||||
values.put("spring", "framework");
|
||||
PropertySource<?> propertySource = new MapPropertySource("test", values);
|
||||
env.getPropertySources().addFirst(propertySource);
|
||||
assertThat(env.getProperty("spring")).isEqualTo("framework-test");
|
||||
}
|
||||
|
||||
private Profiles defaultProfile() {
|
||||
return Profiles.of(AbstractEnvironment.RESERVED_DEFAULT_PROFILE_NAME);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user