|
128 | 128 | * @author Stephane Nicoll
|
129 | 129 | * @author Madhura Bhave
|
130 | 130 | * @author Vladislav Kisel
|
| 131 | + * @author Yanming Zhou |
131 | 132 | */
|
132 | 133 | @ExtendWith(OutputCaptureExtension.class)
|
133 | 134 | class ConfigurationPropertiesTests {
|
@@ -1270,6 +1271,15 @@ void loadWhenBindingToJavaBeanWithConversionToCustomListImplementation() {
|
1270 | 1271 | assertThat(this.context.getBean(SetterBoundCustomListProperties.class).getValues()).containsExactly("a", "b");
|
1271 | 1272 | }
|
1272 | 1273 |
|
| 1274 | + @Test |
| 1275 | + void bindMultipleBeansUsingInheritedPrefix() { |
| 1276 | + load(InheritedPrefixConfiguration.class, "spring.service.host=127.0.0.1", "spring.service.port=6379", |
| 1277 | + "additional.service.port=6380"); |
| 1278 | + ServiceProperties properties = this.context.getBean("additionalServiceProperties", ServiceProperties.class); |
| 1279 | + assertThat(properties.getHost()).isEqualTo("127.0.0.1"); |
| 1280 | + assertThat(properties.getPort()).isEqualTo(6380); |
| 1281 | + } |
| 1282 | + |
1273 | 1283 | private AnnotationConfigApplicationContext load(Class<?> configuration, String... inlinedProperties) {
|
1274 | 1284 | return load(new Class<?>[] { configuration }, inlinedProperties);
|
1275 | 1285 | }
|
@@ -3310,4 +3320,40 @@ static final class CustomList<E> extends ArrayList<E> {
|
3310 | 3320 |
|
3311 | 3321 | }
|
3312 | 3322 |
|
| 3323 | + @ConfigurationProperties(prefix = "spring.service") |
| 3324 | + static class ServiceProperties { |
| 3325 | + |
| 3326 | + private String host = "localhost"; |
| 3327 | + |
| 3328 | + private int port = 6379; |
| 3329 | + |
| 3330 | + String getHost() { |
| 3331 | + return this.host; |
| 3332 | + } |
| 3333 | + |
| 3334 | + void setHost(String host) { |
| 3335 | + this.host = host; |
| 3336 | + } |
| 3337 | + |
| 3338 | + int getPort() { |
| 3339 | + return this.port; |
| 3340 | + } |
| 3341 | + |
| 3342 | + void setPort(int port) { |
| 3343 | + this.port = port; |
| 3344 | + } |
| 3345 | + |
| 3346 | + } |
| 3347 | + |
| 3348 | + @EnableConfigurationProperties(ServiceProperties.class) |
| 3349 | + static class InheritedPrefixConfiguration { |
| 3350 | + |
| 3351 | + @Bean(autowireCandidate = false) // do not back off auto-configured one |
| 3352 | + @ConfigurationProperties(prefix = "additional.service", inheritedPrefix = "spring.service") |
| 3353 | + ServiceProperties additionalServiceProperties() { |
| 3354 | + return new ServiceProperties(); |
| 3355 | + } |
| 3356 | + |
| 3357 | + } |
| 3358 | + |
3313 | 3359 | }
|
0 commit comments