|
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,25 @@ void loadWhenBindingToJavaBeanWithConversionToCustomListImplementation() {
|
1270 | 1271 | assertThat(this.context.getBean(SetterBoundCustomListProperties.class).getValues()).containsExactly("a", "b");
|
1271 | 1272 | }
|
1272 | 1273 |
|
| 1274 | + @Test |
| 1275 | + void loadWhenUsingInheritedPrefixForJavaBeanBinder() { |
| 1276 | + load(SetterBoundInheritedPrefixConfiguration.class, "spring.service.host=127.0.0.1", "spring.service.port=6379", |
| 1277 | + "additional.service.port=6380"); |
| 1278 | + SetterBoundServiceProperties properties = this.context.getBean("additionalServiceProperties", |
| 1279 | + SetterBoundServiceProperties.class); |
| 1280 | + assertThat(properties.getPort()).isEqualTo(6380); |
| 1281 | + assertThat(properties.getHost()).isEqualTo("127.0.0.1"); |
| 1282 | + } |
| 1283 | + |
| 1284 | + @Test |
| 1285 | + void loadWhenUsingInheritedPrefixForValueObjectBinder() { |
| 1286 | + load(ConstructorBoundInheritedPrefixConfiguration.class, "spring.service.host=127.0.0.1", |
| 1287 | + "spring.service.port=6379", "additional.service.port=6380"); |
| 1288 | + ConstructorBoundServiceProperties properties = this.context.getBean(ConstructorBoundServiceProperties.class); |
| 1289 | + assertThat(properties.getPort()).isEqualTo(6380); |
| 1290 | + assertThat(properties.getHost()).isEqualTo("127.0.0.1"); |
| 1291 | + } |
| 1292 | + |
1273 | 1293 | private AnnotationConfigApplicationContext load(Class<?> configuration, String... inlinedProperties) {
|
1274 | 1294 | return load(new Class<?>[] { configuration }, inlinedProperties);
|
1275 | 1295 | }
|
@@ -3310,4 +3330,68 @@ static final class CustomList<E> extends ArrayList<E> {
|
3310 | 3330 |
|
3311 | 3331 | }
|
3312 | 3332 |
|
| 3333 | + @ConfigurationProperties(prefix = "spring.service") |
| 3334 | + static class SetterBoundServiceProperties { |
| 3335 | + |
| 3336 | + private String host = "localhost"; |
| 3337 | + |
| 3338 | + private int port = 6379; |
| 3339 | + |
| 3340 | + String getHost() { |
| 3341 | + return this.host; |
| 3342 | + } |
| 3343 | + |
| 3344 | + void setHost(String host) { |
| 3345 | + this.host = host; |
| 3346 | + } |
| 3347 | + |
| 3348 | + int getPort() { |
| 3349 | + return this.port; |
| 3350 | + } |
| 3351 | + |
| 3352 | + void setPort(int port) { |
| 3353 | + this.port = port; |
| 3354 | + } |
| 3355 | + |
| 3356 | + } |
| 3357 | + |
| 3358 | + @EnableConfigurationProperties(SetterBoundServiceProperties.class) |
| 3359 | + static class SetterBoundInheritedPrefixConfiguration { |
| 3360 | + |
| 3361 | + @Bean(autowireCandidate = false) // do not back off auto-configured one |
| 3362 | + @ConfigurationProperties(prefix = "additional.service", inheritedPrefix = "spring.service") |
| 3363 | + SetterBoundServiceProperties additionalServiceProperties() { |
| 3364 | + return new SetterBoundServiceProperties(); |
| 3365 | + } |
| 3366 | + |
| 3367 | + } |
| 3368 | + |
| 3369 | + @ConfigurationProperties(prefix = "additional.service", inheritedPrefix = "spring.service") |
| 3370 | + static class ConstructorBoundServiceProperties { |
| 3371 | + |
| 3372 | + private final String host; |
| 3373 | + |
| 3374 | + private final int port; |
| 3375 | + |
| 3376 | + public ConstructorBoundServiceProperties(@DefaultValue("localhost") String host, |
| 3377 | + @DefaultValue("6379") int port) { |
| 3378 | + this.host = host; |
| 3379 | + this.port = port; |
| 3380 | + } |
| 3381 | + |
| 3382 | + String getHost() { |
| 3383 | + return this.host; |
| 3384 | + } |
| 3385 | + |
| 3386 | + int getPort() { |
| 3387 | + return this.port; |
| 3388 | + } |
| 3389 | + |
| 3390 | + } |
| 3391 | + |
| 3392 | + @EnableConfigurationProperties(ConstructorBoundServiceProperties.class) |
| 3393 | + static class ConstructorBoundInheritedPrefixConfiguration { |
| 3394 | + |
| 3395 | + } |
| 3396 | + |
3313 | 3397 | }
|
0 commit comments