|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2024 the original author or authors. |
| 2 | + * Copyright 2002-2025 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
29 | 29 | import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
30 | 30 | import org.springframework.context.annotation.Bean;
|
31 | 31 | import org.springframework.context.annotation.Configuration;
|
| 32 | +import org.springframework.core.convert.converter.Converter; |
32 | 33 | import org.springframework.core.convert.support.DefaultConversionService;
|
33 | 34 | import org.springframework.core.env.MutablePropertySources;
|
34 | 35 | import org.springframework.core.env.PropertySource;
|
@@ -72,6 +73,33 @@ void replacementFromEnvironmentProperties() {
|
72 | 73 | assertThat(ppc.getAppliedPropertySources()).isNotNull();
|
73 | 74 | }
|
74 | 75 |
|
| 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 | + |
75 | 103 | @Test
|
76 | 104 | void localPropertiesViaResource() {
|
77 | 105 | DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
|
|
0 commit comments