From b4355dc955ef112fded9129c2fd8feeaa5e253e7 Mon Sep 17 00:00:00 2001 From: Sam Brannen <104798+sbrannen@users.noreply.github.com> Date: Wed, 7 May 2025 12:42:40 +0200 Subject: [PATCH 1/2] Polishing --- .../PropertySourcesPlaceholderConfigurer.java | 12 +++++++----- .../env/PropertySourcesPropertyResolverTests.java | 2 +- 2 files changed, 8 insertions(+), 6 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 31d1652542b..3708ce7ce68 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 @@ -1,5 +1,5 @@ /* - * Copyright 2002-2024 the original author or authors. + * Copyright 2002-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -49,7 +49,7 @@ import org.springframework.util.StringValueResolver; * XSD documentation for complete details. * *

Any local properties (for example, those added via {@link #setProperties}, {@link #setLocations} - * et al.) are added as a {@code PropertySource}. Search precedence of local properties is + * et al.) are added as a single {@link PropertySource}. Search precedence of local properties is * based on the value of the {@link #setLocalOverride localOverride} property, which is by * default {@code false} meaning that local properties are to be searched last, after all * environment property sources. @@ -101,8 +101,9 @@ public class PropertySourcesPlaceholderConfigurer extends PlaceholderConfigurerS } /** - * {@code PropertySources} from the given {@link Environment} - * will be searched when replacing ${...} placeholders. + * {@inheritDoc} + *

{@code PropertySources} from the given {@link Environment} will be searched + * when replacing ${...} placeholders. * @see #setPropertySources * @see #postProcessBeanFactory */ @@ -176,6 +177,7 @@ public class PropertySourcesPlaceholderConfigurer extends PlaceholderConfigurerS /** * Create a {@link ConfigurablePropertyResolver} for the specified property sources. + *

The default implementation creates a {@link PropertySourcesPropertyResolver}. * @param propertySources the property sources to use * @since 6.0.12 */ @@ -188,7 +190,7 @@ public class PropertySourcesPlaceholderConfigurer extends PlaceholderConfigurerS * placeholders with values from the given properties. */ protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, - final ConfigurablePropertyResolver propertyResolver) throws BeansException { + ConfigurablePropertyResolver propertyResolver) throws BeansException { propertyResolver.setPlaceholderPrefix(this.placeholderPrefix); propertyResolver.setPlaceholderSuffix(this.placeholderSuffix); diff --git a/spring-core/src/test/java/org/springframework/core/env/PropertySourcesPropertyResolverTests.java b/spring-core/src/test/java/org/springframework/core/env/PropertySourcesPropertyResolverTests.java index 23654dfe10e..42faac60483 100644 --- a/spring-core/src/test/java/org/springframework/core/env/PropertySourcesPropertyResolverTests.java +++ b/spring-core/src/test/java/org/springframework/core/env/PropertySourcesPropertyResolverTests.java @@ -42,7 +42,7 @@ class PropertySourcesPropertyResolverTests { private MutablePropertySources propertySources; - private ConfigurablePropertyResolver propertyResolver; + private PropertySourcesPropertyResolver propertyResolver; @BeforeEach From 0867dfca33168d9f3871d3d1a2bab6b14f5093f6 Mon Sep 17 00:00:00 2001 From: Sam Brannen <104798+sbrannen@users.noreply.github.com> Date: Wed, 7 May 2025 14:25:13 +0200 Subject: [PATCH 2/2] Introduce CompositePropertySource constructor that accepts Iterable This commit introduces a new constructor for CompositePropertySource that accepts a `String name` and an Iterable>, which allows a CompositePropertySource to be constructed from existing property sources, such as an instance of MutablePropertySources. Closes gh-34862 --- .../core/env/CompositePropertySource.java | 29 +++++++++++++++++-- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/core/env/CompositePropertySource.java b/spring-core/src/main/java/org/springframework/core/env/CompositePropertySource.java index 5fe8115842c..3371737b0d1 100644 --- a/spring-core/src/main/java/org/springframework/core/env/CompositePropertySource.java +++ b/spring-core/src/main/java/org/springframework/core/env/CompositePropertySource.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2024 the original author or authors. + * Copyright 2002-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -39,6 +39,7 @@ import org.springframework.util.StringUtils; * @author Chris Beams * @author Juergen Hoeller * @author Phillip Webb + * @author Sam Brannen * @since 3.1.1 */ public class CompositePropertySource extends EnumerablePropertySource { @@ -47,13 +48,35 @@ public class CompositePropertySource extends EnumerablePropertySource { /** - * Create a new {@code CompositePropertySource}. - * @param name the name of the property source + * Create a new empty {@code CompositePropertySource} with the given name. + * @param name the name of the composite property source + * @see #CompositePropertySource(String, Iterable) + * @see #addPropertySource(PropertySource) + * @see #addFirstPropertySource(PropertySource) */ public CompositePropertySource(String name) { super(name); } + /** + * Create a new {@code CompositePropertySource} with the given name and + * property sources supplied as an {@link Iterable} or {@link PropertySources} + * implementation, preserving the original order of the property sources. + * @param name the name of the composite property source + * @param propertySources the initial set of {@link PropertySource} instances + * @since 6.2.7 + * @see PropertySources + * @see MutablePropertySources + * @see #addPropertySource(PropertySource) + * @see #addFirstPropertySource(PropertySource) + */ + public CompositePropertySource(String name, Iterable> propertySources) { + this(name); + for (PropertySource propertySource : propertySources) { + this.propertySources.add(propertySource); + } + } + @Override @Nullable