|
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 autowiredConstructorShouldInjectBeanInsteadOfBindProperties() { |
| 1276 | + load(ServicePropertiesConfiguration.class, "primary.service.port=1234", "foo.service.host=127.0.0.1"); |
| 1277 | + PrimaryServiceProperties primaryServiceProperties = this.context.getBean(PrimaryServiceProperties.class); |
| 1278 | + assertThat(primaryServiceProperties.getHost()).isEqualTo("localhost"); |
| 1279 | + assertThat(primaryServiceProperties.getPort()).isEqualTo(1234); |
| 1280 | + FooServiceProperties fooServiceProperties = this.context.getBean(FooServiceProperties.class); |
| 1281 | + assertThat(fooServiceProperties.getHost()).isEqualTo("127.0.0.1"); |
| 1282 | + assertThat(fooServiceProperties.getPort()).isEqualTo(1234); |
| 1283 | + } |
| 1284 | + |
| 1285 | + @Test |
| 1286 | + void privateConstructorShouldInjectBeanInsteadOfBindProperties() { |
| 1287 | + load(ServicePropertiesConfiguration.class, "primary.service.port=1234", "bar.service.host=127.0.0.1"); |
| 1288 | + BarServiceProperties barServiceProperties = this.context.getBean(BarServiceProperties.class); |
| 1289 | + assertThat(barServiceProperties.getHost()).isEqualTo("127.0.0.1"); |
| 1290 | + assertThat(barServiceProperties.getPort()).isEqualTo(1234); |
| 1291 | + } |
| 1292 | + |
1273 | 1293 | private AnnotationConfigApplicationContext load(Class<?> configuration, String... inlinedProperties) {
|
1274 | 1294 | return load(new Class<?>[] { configuration }, inlinedProperties);
|
1275 | 1295 | }
|
@@ -3310,4 +3330,61 @@ static final class CustomList<E> extends ArrayList<E> {
|
3310 | 3330 |
|
3311 | 3331 | }
|
3312 | 3332 |
|
| 3333 | + static abstract class ServiceProperties { |
| 3334 | + |
| 3335 | + private String host = "localhost"; |
| 3336 | + |
| 3337 | + private int port = 1000; |
| 3338 | + |
| 3339 | + public String getHost() { |
| 3340 | + return this.host; |
| 3341 | + } |
| 3342 | + |
| 3343 | + public void setHost(String host) { |
| 3344 | + this.host = host; |
| 3345 | + } |
| 3346 | + |
| 3347 | + public int getPort() { |
| 3348 | + return this.port; |
| 3349 | + } |
| 3350 | + |
| 3351 | + public void setPort(int port) { |
| 3352 | + this.port = port; |
| 3353 | + } |
| 3354 | + |
| 3355 | + } |
| 3356 | + |
| 3357 | + @ConfigurationProperties("primary.service") |
| 3358 | + static class PrimaryServiceProperties extends ServiceProperties { |
| 3359 | + |
| 3360 | + } |
| 3361 | + |
| 3362 | + @ConfigurationProperties("foo.service") |
| 3363 | + static class FooServiceProperties extends ServiceProperties { |
| 3364 | + |
| 3365 | + @Autowired |
| 3366 | + public FooServiceProperties(PrimaryServiceProperties serviceProperties) { |
| 3367 | + // should use BeanUtils.copyProperties(serviceProperties, this) in practice |
| 3368 | + setHost(serviceProperties.getHost()); |
| 3369 | + setPort(serviceProperties.getPort()); |
| 3370 | + } |
| 3371 | + |
| 3372 | + } |
| 3373 | + |
| 3374 | + @ConfigurationProperties("bar.service") |
| 3375 | + static class BarServiceProperties extends ServiceProperties { |
| 3376 | + |
| 3377 | + private BarServiceProperties(PrimaryServiceProperties serviceProperties) { |
| 3378 | + setHost(serviceProperties.getHost()); |
| 3379 | + setPort(serviceProperties.getPort()); |
| 3380 | + } |
| 3381 | + |
| 3382 | + } |
| 3383 | + |
| 3384 | + @EnableConfigurationProperties({ PrimaryServiceProperties.class, FooServiceProperties.class, |
| 3385 | + BarServiceProperties.class }) |
| 3386 | + static class ServicePropertiesConfiguration { |
| 3387 | + |
| 3388 | + } |
| 3389 | + |
3313 | 3390 | }
|
0 commit comments