Skip to content

Commit

Permalink
Refactor PropertySourcesDeducer
Browse files Browse the repository at this point in the history
Prior to the changes that fixed #12451,
`FilteredPropertySources` and `CompositePropertySources` were required
by the `PropertySourcesDeducer` to ensure that configuration properties binding
could see changes to the environment even when there was a
PropertySourcesPlaceholderConfigurer in the context. #12451 changed the way
property sources are adapted by `SpringConfigurationPropertySources`, removing the
need for `FilteredPropertySources` and `CompositePropertySources`.

Fixes gh-13738
  • Loading branch information
mbhave committed Aug 2, 2018
1 parent 7e2494e commit 16aff4c
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 394 deletions.

This file was deleted.

This file was deleted.

Expand Up @@ -27,7 +27,6 @@
import org.springframework.core.env.Environment;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertySources;
import org.springframework.util.Assert;

/**
* Utility to deduce the {@link PropertySources} to use for configuration binding.
Expand All @@ -45,20 +44,16 @@ class PropertySourcesDeducer {
}

public PropertySources getPropertySources() {
MutablePropertySources environmentPropertySources = extractEnvironmentPropertySources();
PropertySourcesPlaceholderConfigurer placeholderConfigurer = getSinglePropertySourcesPlaceholderConfigurer();
if (placeholderConfigurer == null) {
Assert.state(environmentPropertySources != null,
"Unable to obtain PropertySources from "
+ "PropertySourcesPlaceholderConfigurer or Environment");
return environmentPropertySources;
PropertySourcesPlaceholderConfigurer configurer = getSinglePropertySourcesPlaceholderConfigurer();
if (configurer != null) {
return configurer.getAppliedPropertySources();
}
PropertySources appliedPropertySources = placeholderConfigurer
.getAppliedPropertySources();
if (environmentPropertySources == null) {
return appliedPropertySources;
MutablePropertySources sources = extractEnvironmentPropertySources();
if (sources != null) {
return sources;
}
return merge(environmentPropertySources, appliedPropertySources);
throw new IllegalStateException("Unable to obtain PropertySources from "
+ "PropertySourcesPlaceholderConfigurer or Environment");
}

private MutablePropertySources extractEnvironmentPropertySources() {
Expand All @@ -84,12 +79,4 @@ private PropertySourcesPlaceholderConfigurer getSinglePropertySourcesPlaceholder
return null;
}

private PropertySources merge(PropertySources environmentPropertySources,
PropertySources appliedPropertySources) {
FilteredPropertySources filtered = new FilteredPropertySources(
appliedPropertySources,
PropertySourcesPlaceholderConfigurer.ENVIRONMENT_PROPERTIES_PROPERTY_SOURCE_NAME);
return new CompositePropertySources(filtered, environmentPropertySources);
}

}

This file was deleted.

Expand Up @@ -25,6 +25,7 @@
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import javax.annotation.PostConstruct;
Expand Down Expand Up @@ -352,6 +353,15 @@ public void loadWithPropertyPlaceholderValueShouldBind() {
assertThat(bean.getValue()).isEqualTo("foo");
}

@Test
public void loadWithPropertyPlaceholderShouldNotAlterPropertySourceOrder() {
load(WithPropertyPlaceholderWithLocalPropertiesValueConfiguration.class,
"com.example.bar=a");
SimplePrefixedProperties bean = this.context
.getBean(SimplePrefixedProperties.class);
assertThat(bean.getBar()).isEqualTo("a");
}

@Test
public void loadWhenHasPostConstructShouldTriggerPostConstructWithBoundBean() {
MockEnvironment environment = new MockEnvironment();
Expand Down Expand Up @@ -959,6 +969,21 @@ public static PropertySourcesPlaceholderConfigurer configurer() {

}

@Configuration
@EnableConfigurationProperties(SimplePrefixedProperties.class)
static class WithPropertyPlaceholderWithLocalPropertiesValueConfiguration {

@Bean
public static PropertySourcesPlaceholderConfigurer configurer() {
PropertySourcesPlaceholderConfigurer placeholderConfigurer = new PropertySourcesPlaceholderConfigurer();
Properties properties = new Properties();
properties.put("com.example.bar", "b");
placeholderConfigurer.setProperties(properties);
return placeholderConfigurer;
}

}

@Configuration
@EnableConfigurationProperties
static class WithFactoryBeanConfiguration {
Expand Down

0 comments on commit 16aff4c

Please sign in to comment.