Skip to content

Commit 16aff4c

Browse files
committed
Refactor PropertySourcesDeducer
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
1 parent 7e2494e commit 16aff4c

File tree

6 files changed

+33
-394
lines changed

6 files changed

+33
-394
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/CompositePropertySources.java

Lines changed: 0 additions & 70 deletions
This file was deleted.

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/FilteredPropertySources.java

Lines changed: 0 additions & 74 deletions
This file was deleted.

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/PropertySourcesDeducer.java

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import org.springframework.core.env.Environment;
2828
import org.springframework.core.env.MutablePropertySources;
2929
import org.springframework.core.env.PropertySources;
30-
import org.springframework.util.Assert;
3130

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

4746
public PropertySources getPropertySources() {
48-
MutablePropertySources environmentPropertySources = extractEnvironmentPropertySources();
49-
PropertySourcesPlaceholderConfigurer placeholderConfigurer = getSinglePropertySourcesPlaceholderConfigurer();
50-
if (placeholderConfigurer == null) {
51-
Assert.state(environmentPropertySources != null,
52-
"Unable to obtain PropertySources from "
53-
+ "PropertySourcesPlaceholderConfigurer or Environment");
54-
return environmentPropertySources;
47+
PropertySourcesPlaceholderConfigurer configurer = getSinglePropertySourcesPlaceholderConfigurer();
48+
if (configurer != null) {
49+
return configurer.getAppliedPropertySources();
5550
}
56-
PropertySources appliedPropertySources = placeholderConfigurer
57-
.getAppliedPropertySources();
58-
if (environmentPropertySources == null) {
59-
return appliedPropertySources;
51+
MutablePropertySources sources = extractEnvironmentPropertySources();
52+
if (sources != null) {
53+
return sources;
6054
}
61-
return merge(environmentPropertySources, appliedPropertySources);
55+
throw new IllegalStateException("Unable to obtain PropertySources from "
56+
+ "PropertySourcesPlaceholderConfigurer or Environment");
6257
}
6358

6459
private MutablePropertySources extractEnvironmentPropertySources() {
@@ -84,12 +79,4 @@ private PropertySourcesPlaceholderConfigurer getSinglePropertySourcesPlaceholder
8479
return null;
8580
}
8681

87-
private PropertySources merge(PropertySources environmentPropertySources,
88-
PropertySources appliedPropertySources) {
89-
FilteredPropertySources filtered = new FilteredPropertySources(
90-
appliedPropertySources,
91-
PropertySourcesPlaceholderConfigurer.ENVIRONMENT_PROPERTIES_PROPERTY_SOURCE_NAME);
92-
return new CompositePropertySources(filtered, environmentPropertySources);
93-
}
94-
9582
}

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/CompositePropertySourcesTests.java

Lines changed: 0 additions & 144 deletions
This file was deleted.

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesTests.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.util.LinkedHashMap;
2626
import java.util.List;
2727
import java.util.Map;
28+
import java.util.Properties;
2829
import java.util.Set;
2930

3031
import javax.annotation.PostConstruct;
@@ -352,6 +353,15 @@ public void loadWithPropertyPlaceholderValueShouldBind() {
352353
assertThat(bean.getValue()).isEqualTo("foo");
353354
}
354355

356+
@Test
357+
public void loadWithPropertyPlaceholderShouldNotAlterPropertySourceOrder() {
358+
load(WithPropertyPlaceholderWithLocalPropertiesValueConfiguration.class,
359+
"com.example.bar=a");
360+
SimplePrefixedProperties bean = this.context
361+
.getBean(SimplePrefixedProperties.class);
362+
assertThat(bean.getBar()).isEqualTo("a");
363+
}
364+
355365
@Test
356366
public void loadWhenHasPostConstructShouldTriggerPostConstructWithBoundBean() {
357367
MockEnvironment environment = new MockEnvironment();
@@ -959,6 +969,21 @@ public static PropertySourcesPlaceholderConfigurer configurer() {
959969

960970
}
961971

972+
@Configuration
973+
@EnableConfigurationProperties(SimplePrefixedProperties.class)
974+
static class WithPropertyPlaceholderWithLocalPropertiesValueConfiguration {
975+
976+
@Bean
977+
public static PropertySourcesPlaceholderConfigurer configurer() {
978+
PropertySourcesPlaceholderConfigurer placeholderConfigurer = new PropertySourcesPlaceholderConfigurer();
979+
Properties properties = new Properties();
980+
properties.put("com.example.bar", "b");
981+
placeholderConfigurer.setProperties(properties);
982+
return placeholderConfigurer;
983+
}
984+
985+
}
986+
962987
@Configuration
963988
@EnableConfigurationProperties
964989
static class WithFactoryBeanConfiguration {

0 commit comments

Comments
 (0)