Skip to content

Commit bccc9c1

Browse files
committed
Refine ConfigurationPropertySources attach logic
Refine `ConfigurationPropertySources.attach` logic to endure that the attached or reattached source is always first. See gh-29409
1 parent 79d9549 commit bccc9c1

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,17 @@ public static void attach(Environment environment) {
8787
Assert.isInstanceOf(ConfigurableEnvironment.class, environment);
8888
MutablePropertySources sources = ((ConfigurableEnvironment) environment).getPropertySources();
8989
PropertySource<?> attached = getAttached(sources);
90-
if (attached != null) {
91-
if (attached instanceof ConfigurationPropertySourcesPropertySource
92-
&& ((SpringConfigurationPropertySources) attached.getSource()).isUsingSources(sources)) {
93-
return;
94-
}
95-
sources.remove(ATTACHED_PROPERTY_SOURCE_NAME);
90+
if (attached == null || !isUsingSources(attached, sources)) {
91+
attached = new ConfigurationPropertySourcesPropertySource(ATTACHED_PROPERTY_SOURCE_NAME,
92+
new SpringConfigurationPropertySources(sources));
9693
}
97-
sources.addFirst(new ConfigurationPropertySourcesPropertySource(ATTACHED_PROPERTY_SOURCE_NAME,
98-
new SpringConfigurationPropertySources(sources)));
94+
sources.remove(ATTACHED_PROPERTY_SOURCE_NAME);
95+
sources.addFirst(attached);
96+
}
97+
98+
private static boolean isUsingSources(PropertySource<?> attached, MutablePropertySources sources) {
99+
return attached instanceof ConfigurationPropertySourcesPropertySource
100+
&& ((SpringConfigurationPropertySources) attached.getSource()).isUsingSources(sources);
99101
}
100102

101103
static PropertySource<?> getAttached(MutablePropertySources sources) {

0 commit comments

Comments
 (0)