Skip to content

Commit 90be94a

Browse files
committed
Use ConversionService from Environment in PropertySourcesPlaceholderConfigurer
This commit fixes a regression in PropertySourcesPlaceholderConfigurer that was introduced in Spring Framework 6.2.7. Specifically, this commit reinstates automatic String-conversion of values from PropertySources in the Environment using the ConversionService configured in the Environment. See gh-34861 Closes gh-34936
1 parent d9e261a commit 90be94a

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

spring-context/src/main/java/org/springframework/context/support/PropertySourcesPlaceholderConfigurer.java

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
2525
import org.springframework.beans.factory.config.PlaceholderConfigurerSupport;
2626
import org.springframework.context.EnvironmentAware;
27+
import org.springframework.core.convert.ConversionService;
28+
import org.springframework.core.convert.support.DefaultConversionService;
2729
import org.springframework.core.env.ConfigurableEnvironment;
2830
import org.springframework.core.env.ConfigurablePropertyResolver;
2931
import org.springframework.core.env.Environment;
@@ -243,16 +245,38 @@ public boolean containsProperty(String name) {
243245

244246
@Override
245247
@Nullable
246-
public Object getProperty(String name) {
248+
// Declare String as covariant return type, since a String is actually required.
249+
public String getProperty(String name) {
247250
for (PropertySource<?> propertySource : super.source.getPropertySources()) {
248251
Object candidate = propertySource.getProperty(name);
249252
if (candidate != null) {
250-
return candidate;
253+
return convertToString(candidate);
251254
}
252255
}
253256
return null;
254257
}
255258

259+
/**
260+
* Convert the supplied value to a {@link String} using the {@link ConversionService}
261+
* from the {@link Environment}.
262+
* <p>This is a modified version of
263+
* {@link org.springframework.core.env.AbstractPropertyResolver#convertValueIfNecessary(Object, Class)}.
264+
* @param value the value to convert
265+
* @return the converted value, or the original value if no conversion is necessary
266+
* @since 6.2.8
267+
*/
268+
@Nullable
269+
private String convertToString(Object value) {
270+
if (value instanceof String string) {
271+
return string;
272+
}
273+
ConversionService conversionService = super.source.getConversionService();
274+
if (conversionService == null) {
275+
conversionService = DefaultConversionService.getSharedInstance();
276+
}
277+
return conversionService.convert(value, String.class);
278+
}
279+
256280
@Override
257281
public String toString() {
258282
return "ConfigurableEnvironmentPropertySource {propertySources=" + super.source.getPropertySources() + "}";
@@ -279,7 +303,8 @@ public boolean containsProperty(String name) {
279303

280304
@Override
281305
@Nullable
282-
public Object getProperty(String name) {
306+
// Declare String as covariant return type, since a String is actually required.
307+
public String getProperty(String name) {
283308
return super.source.getProperty(name);
284309
}
285310

spring-context/src/test/java/org/springframework/context/support/PropertySourcesPlaceholderConfigurerTests.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525

2626
import org.junit.jupiter.api.AfterAll;
2727
import org.junit.jupiter.api.BeforeEach;
28-
import org.junit.jupiter.api.Disabled;
2928
import org.junit.jupiter.api.Nested;
3029
import org.junit.jupiter.api.Test;
3130
import org.junit.jupiter.params.ParameterizedTest;
@@ -98,7 +97,6 @@ void replacementFromEnvironmentProperties() {
9897
* used by the {@code Environment} is applied during placeholder resolution
9998
* against a {@link PropertySource} registered in the {@code Environment}.
10099
*/
101-
@Disabled("Disabled until gh-34936 is resolved")
102100
@Test // gh-34936
103101
void replacementFromEnvironmentPropertiesWithConversion() {
104102
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();

0 commit comments

Comments
 (0)