From 71355277652f6675b44ff9bf1a9d1ca7f132b889 Mon Sep 17 00:00:00 2001 From: Sam Brannen <104798+sbrannen@users.noreply.github.com> Date: Sun, 11 May 2025 15:26:50 +0200 Subject: [PATCH] Always obtain fresh PropertySources in ConfigurableEnvironmentPropertySource Although it's unlikely that the implementation of getPropertySources() in a ConfigurableEnvironment would be overridden to return a different MutablePropertySources instance than the one that the ConfigurableEnvironment typically acts on, it is in fact possible. In light of that possibility, this commit refactors ConfigurableEnvironmentPropertySource so that it always obtains a fresh PropertySources reference. See gh-34861 --- .../support/PropertySourcesPlaceholderConfigurer.java | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/spring-context/src/main/java/org/springframework/context/support/PropertySourcesPlaceholderConfigurer.java b/spring-context/src/main/java/org/springframework/context/support/PropertySourcesPlaceholderConfigurer.java index f5a6d3c33f..4e023ab985 100644 --- a/spring-context/src/main/java/org/springframework/context/support/PropertySourcesPlaceholderConfigurer.java +++ b/spring-context/src/main/java/org/springframework/context/support/PropertySourcesPlaceholderConfigurer.java @@ -227,19 +227,15 @@ public class PropertySourcesPlaceholderConfigurer extends PlaceholderConfigurerS */ private static class ConfigurableEnvironmentPropertySource extends PropertySource { - private final PropertySources propertySources; - - ConfigurableEnvironmentPropertySource(ConfigurableEnvironment environment) { super(ENVIRONMENT_PROPERTIES_PROPERTY_SOURCE_NAME, environment); - this.propertySources = environment.getPropertySources(); } @Override @Nullable public Object getProperty(String name) { - for (PropertySource propertySource : this.propertySources) { + for (PropertySource propertySource : super.source.getPropertySources()) { Object candidate = propertySource.getProperty(name); if (candidate != null) { return candidate; @@ -250,7 +246,7 @@ public class PropertySourcesPlaceholderConfigurer extends PlaceholderConfigurerS @Override public boolean containsProperty(String name) { - for (PropertySource propertySource : this.propertySources) { + for (PropertySource propertySource : super.source.getPropertySources()) { if (propertySource.containsProperty(name)) { return true; } @@ -260,7 +256,7 @@ public class PropertySourcesPlaceholderConfigurer extends PlaceholderConfigurerS @Override public String toString() { - return "ConfigurableEnvironmentPropertySource {propertySources=" + this.propertySources + "}"; + return "ConfigurableEnvironmentPropertySource {propertySources=" + super.source.getPropertySources() + "}"; } }