Skip to content

Commit 59ffbd7

Browse files
committed
Test conversion support in PropertySourcesPlaceholderConfigurer
This commit introduces a "regression test" which demonstrates that PropertySourcesPlaceholderConfigurer uses the ConversionService from the Environment. See gh-34936
1 parent c0a9da6 commit 59ffbd7

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

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

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -29,6 +29,7 @@
2929
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
3030
import org.springframework.context.annotation.Bean;
3131
import org.springframework.context.annotation.Configuration;
32+
import org.springframework.core.convert.converter.Converter;
3233
import org.springframework.core.convert.support.DefaultConversionService;
3334
import org.springframework.core.env.MutablePropertySources;
3435
import org.springframework.core.env.PropertySource;
@@ -72,6 +73,33 @@ void replacementFromEnvironmentProperties() {
7273
assertThat(ppc.getAppliedPropertySources()).isNotNull();
7374
}
7475

76+
@Test // gh-34936
77+
void replacementFromEnvironmentPropertiesWithConversion() {
78+
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
79+
bf.registerBeanDefinition("testBean",
80+
genericBeanDefinition(TestBean.class)
81+
.addPropertyValue("name", "${my.name}")
82+
.getBeanDefinition());
83+
84+
record Point(int x, int y) {
85+
}
86+
87+
Converter<Point, String> pointToStringConverter =
88+
point -> "(%d,%d)".formatted(point.x, point.y);
89+
90+
DefaultConversionService conversionService = new DefaultConversionService();
91+
conversionService.addConverter(Point.class, String.class, pointToStringConverter);
92+
93+
MockEnvironment env = new MockEnvironment();
94+
env.setConversionService(conversionService);
95+
env.setProperty("my.name", new Point(4,5));
96+
97+
PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer();
98+
ppc.setEnvironment(env);
99+
ppc.postProcessBeanFactory(bf);
100+
assertThat(bf.getBean(TestBean.class).getName()).isEqualTo("(4,5)");
101+
}
102+
75103
@Test
76104
void localPropertiesViaResource() {
77105
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();

0 commit comments

Comments
 (0)