From ebffa493e4021b905dcba114958194caa3ac6641 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Tue, 9 Feb 2016 17:19:11 +0100 Subject: [PATCH] Harmonize ConfigurationProperties bean name When `@EnableConfigurationProperties` is defined, Spring Boot automatically registers a bean in the context for each class specified on the annotation. Previously, the name of the bean only included the prefix which leads to conflict if two different classes use the same prefix. This commit changes the bean name structure to be - where prefix is the prefix used on the annotation and the fully qualified name of the target class. Closes gh-4395 --- .../src/main/asciidoc/configprops.adoc | 4 ++-- .../autoconfigure/MetricExportAutoConfiguration.java | 4 ++-- .../src/main/asciidoc/spring-boot-features.adoc | 12 ++++++++++++ .../actuator/SampleActuatorApplicationTests.java | 5 +++-- .../EnableConfigurationPropertiesImportSelector.java | 4 ++-- .../EnableConfigurationPropertiesTests.java | 5 ++++- 6 files changed, 25 insertions(+), 9 deletions(-) diff --git a/spring-boot-actuator-docs/src/main/asciidoc/configprops.adoc b/spring-boot-actuator-docs/src/main/asciidoc/configprops.adoc index c68f452ecd1..20d69bb997c 100644 --- a/spring-boot-actuator-docs/src/main/asciidoc/configprops.adoc +++ b/spring-boot-actuator-docs/src/main/asciidoc/configprops.adoc @@ -3,8 +3,8 @@ This endpoint is a report on the Spring Boot `@ConfigurationProperties` beans. B this annotation are bound to the `Environment` on startup, so they reflect the externalised configuration of the application. Beans are listed by name. A bean that is added using `@EnableConfigurationProperties` will have a conventional name: -`.CONFIGURATION_PROPERTIES`, where `` is the environment key prefix -specified in the `@ConfigurationProperties` annotation. +`-`, where `` is the environment key prefix specified in the +`@ConfigurationProperties` annotation and the fully qualified name of the bean. Example curl request: include::{generated}/configprops/curl-request.adoc[] diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/MetricExportAutoConfiguration.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/MetricExportAutoConfiguration.java index eb4b71f4623..a92f69d6070 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/MetricExportAutoConfiguration.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/MetricExportAutoConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2015 the original author or authors. + * Copyright 2012-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -112,7 +112,7 @@ public class MetricExportAutoConfiguration { private String aggregateKeyPattern = "k.d"; - @Bean(name = "spring.metrics.export.CONFIGURATION_PROPERTIES") + @Bean(name = "spring.metrics.export-org.springframework.boot.actuate.metrics.export.MetricExportProperties") @ConditionalOnMissingBean public MetricExportProperties metricExportProperties() { MetricExportProperties export = new MetricExportProperties(); diff --git a/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc b/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc index 57c77b3bc8b..fd0080cd9e5 100644 --- a/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc +++ b/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc @@ -762,6 +762,18 @@ definitions by simply listing the properties classes directly in the } ---- +[NOTE] +==== +When `@ConfigurationProperties` bean are registered that way, the bean will have a +conventional name: `-`, where `` is the environment key prefix +specified in the `@ConfigurationProperties` annotation and the fully qualified +name of the bean. If the annotation does not provide any prefix, only the fully qualified +name of the bean is used. + +The bean name in the example above will be `connection-com.example.ConnectionSettings`, +assuming that `ConnectionSettings` sits in the `com.example` package. +==== + TIP: Using `@ConfigurationProperties` also allows you to generate meta-data files that can be used by IDEs. See the <> appendix for details. diff --git a/spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/SampleActuatorApplicationTests.java b/spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/SampleActuatorApplicationTests.java index cbfc1be3964..2277acbfc3d 100644 --- a/spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/SampleActuatorApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/SampleActuatorApplicationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2014 the original author or authors. + * Copyright 2012-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -25,6 +25,7 @@ import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties; import org.springframework.boot.autoconfigure.security.SecurityProperties; import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.boot.test.TestRestTemplate; @@ -219,7 +220,7 @@ public class SampleActuatorApplicationTests { assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); @SuppressWarnings("unchecked") Map body = entity.getBody(); - assertThat(body).containsKey("spring.datasource.CONFIGURATION_PROPERTIES"); + assertThat(body).containsKey("spring.datasource-" + DataSourceProperties.class.getName()); } private String getPassword() { diff --git a/spring-boot/src/main/java/org/springframework/boot/context/properties/EnableConfigurationPropertiesImportSelector.java b/spring-boot/src/main/java/org/springframework/boot/context/properties/EnableConfigurationPropertiesImportSelector.java index e30687f17e5..6489d894aa0 100644 --- a/spring-boot/src/main/java/org/springframework/boot/context/properties/EnableConfigurationPropertiesImportSelector.java +++ b/spring-boot/src/main/java/org/springframework/boot/context/properties/EnableConfigurationPropertiesImportSelector.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2015 the original author or authors. + * Copyright 2012-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -77,7 +77,7 @@ class EnableConfigurationPropertiesImportSelector implements ImportSelector { for (Class type : types) { String prefix = extractPrefix(type); String name = (StringUtils.hasText(prefix) - ? prefix + ".CONFIGURATION_PROPERTIES" : type.getName()); + ? prefix + "-" + type.getName() : type.getName()); if (!registry.containsBeanDefinition(name)) { registerBeanDefinition(registry, type, name); } diff --git a/spring-boot/src/test/java/org/springframework/boot/context/properties/EnableConfigurationPropertiesTests.java b/spring-boot/src/test/java/org/springframework/boot/context/properties/EnableConfigurationPropertiesTests.java index ec162511258..55a7cdbc560 100644 --- a/spring-boot/src/test/java/org/springframework/boot/context/properties/EnableConfigurationPropertiesTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/context/properties/EnableConfigurationPropertiesTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2015 the original author or authors. + * Copyright 2012-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -67,6 +67,7 @@ public class EnableConfigurationPropertiesTests { EnvironmentTestUtils.addEnvironment(this.context, "name:foo"); this.context.refresh(); assertThat(this.context.getBeanNamesForType(TestProperties.class)).hasSize(1); + assertThat(this.context.containsBean(TestProperties.class.getName())).isTrue(); assertThat(this.context.getBean(TestProperties.class).name).isEqualTo("foo"); } @@ -366,6 +367,8 @@ public class EnableConfigurationPropertiesTests { EnvironmentTestUtils.addEnvironment(this.context, "external.name:foo"); this.context.register(AnotherExampleConfig.class); this.context.refresh(); + assertThat(this.context.containsBean("external-" + External.class.getName())) + .isTrue(); assertThat(this.context.getBean(External.class).getName()).isEqualTo("foo"); }