Refine ConfigurationPropertySources attach logic

Refine `ConfigurationPropertySources.attach` logic to endure that
the attached or reattached source is always first.

See gh-29409
pull/29441/head
Phillip Webb 3 years ago
parent 79d9549805
commit bccc9c1861

@ -87,15 +87,17 @@ public final class ConfigurationPropertySources {
Assert.isInstanceOf(ConfigurableEnvironment.class, environment); Assert.isInstanceOf(ConfigurableEnvironment.class, environment);
MutablePropertySources sources = ((ConfigurableEnvironment) environment).getPropertySources(); MutablePropertySources sources = ((ConfigurableEnvironment) environment).getPropertySources();
PropertySource<?> attached = getAttached(sources); PropertySource<?> attached = getAttached(sources);
if (attached != null) { if (attached == null || !isUsingSources(attached, sources)) {
if (attached instanceof ConfigurationPropertySourcesPropertySource attached = new ConfigurationPropertySourcesPropertySource(ATTACHED_PROPERTY_SOURCE_NAME,
&& ((SpringConfigurationPropertySources) attached.getSource()).isUsingSources(sources)) { new SpringConfigurationPropertySources(sources));
return;
} }
sources.remove(ATTACHED_PROPERTY_SOURCE_NAME); sources.remove(ATTACHED_PROPERTY_SOURCE_NAME);
sources.addFirst(attached);
} }
sources.addFirst(new ConfigurationPropertySourcesPropertySource(ATTACHED_PROPERTY_SOURCE_NAME,
new SpringConfigurationPropertySources(sources))); private static boolean isUsingSources(PropertySource<?> attached, MutablePropertySources sources) {
return attached instanceof ConfigurationPropertySourcesPropertySource
&& ((SpringConfigurationPropertySources) attached.getSource()).isUsingSources(sources);
} }
static PropertySource<?> getAttached(MutablePropertySources sources) { static PropertySource<?> getAttached(MutablePropertySources sources) {

Loading…
Cancel
Save