Commit ebffa493 authored by Stephane Nicoll's avatar Stephane Nicoll

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 <prefix>-<fqn> where
prefix is the prefix used on the annotation and <fqn> the fully qualified
name of the target class.

Closes gh-4395
parent 2b18e99a
......@@ -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:
`<prefix>.CONFIGURATION_PROPERTIES`, where `<prefix>` is the environment key prefix
specified in the `@ConfigurationProperties` annotation.
`<prefix>-<fqn>`, where `<prefix>` is the environment key prefix specified in the
`@ConfigurationProperties` annotation and <fqn> the fully qualified name of the bean.
Example curl request:
include::{generated}/configprops/curl-request.adoc[]
......
/*
* 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();
......
......@@ -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: `<prefix>-<fqn>`, where `<prefix>` is the environment key prefix
specified in the `@ConfigurationProperties` annotation and <fqn> 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 <<configuration-metadata>> appendix for details.
......
/*
* 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<String, Object> body = entity.getBody();
assertThat(body).containsKey("spring.datasource.CONFIGURATION_PROPERTIES");
assertThat(body).containsKey("spring.datasource-" + DataSourceProperties.class.getName());
}
private String getPassword() {
......
/*
* 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);
}
......
/*
* 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");
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment