Auto-configure @EnableConfigurationProperties

Add an auto-configuration that avoid a Spring Boot user to add
`@EnableConfigurationProperties` to its configuration to benefit from
that feature.

As our own auto-configurations are tested independently, such annotation
is still present to avoid a useless reference to this new
auto-configuration.

Closes gh-2457
This commit is contained in:
Stephane Nicoll 2015-08-04 13:59:57 +02:00
parent 0e8812f0e9
commit bfe930a873
6 changed files with 144 additions and 5 deletions

View File

@ -0,0 +1,37 @@
/*
* Copyright 2012-2015 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.autoconfigure.context;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
/**
* {@link EnableAutoConfiguration Auto-configuration} for {@link ConfigurationProperties}
* annotated beans. Automatically any bean annotated with {@code @ConfigurationProperties}.
*
* @author Stephane Nicoll
* @since 1.3.0
* @see EnableConfigurationProperties
* @see ConfigurationProperties
*/
@Configuration
@EnableConfigurationProperties
public class ConfigurationPropertiesAutoConfiguration {
}

View File

@ -0,0 +1,20 @@
/*
* Copyright 2012-2015 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Auto-configuration for the Spring context.
*/
package org.springframework.boot.autoconfigure.context;

View File

@ -12,6 +12,7 @@ org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration,\
org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration,\ org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration,\
org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration,\ org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration,\
org.springframework.boot.autoconfigure.cloud.CloudAutoConfiguration,\ org.springframework.boot.autoconfigure.cloud.CloudAutoConfiguration,\
org.springframework.boot.autoconfigure.context.ConfigurationPropertiesAutoConfiguration,\
org.springframework.boot.autoconfigure.dao.PersistenceExceptionTranslationAutoConfiguration,\ org.springframework.boot.autoconfigure.dao.PersistenceExceptionTranslationAutoConfiguration,\
org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchRepositoriesAutoConfiguration,\ org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration,\ org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration,\

View File

@ -0,0 +1,83 @@
/*
* Copyright 2012-2015 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.autoconfigure.context;
import org.junit.After;
import org.junit.Test;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.test.EnvironmentTestUtils;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.stereotype.Component;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
/**
* Tests for {@link ConfigurationPropertiesAutoConfiguration}.
*
* @author Stephane Nicoll
*/
public class ConfigurationPropertiesAutoConfigurationTests {
private AnnotationConfigApplicationContext context;
@After
public void tearDown() {
if (this.context != null) {
this.context.close();
}
}
@Test
public void processAnnotatedBean() {
load(new Class[] {SampleBean.class, ConfigurationPropertiesAutoConfiguration.class},
"foo.name:test");
assertThat(this.context.getBean(SampleBean.class).getName(), is("test"));
}
@Test
public void processAnnotatedBeanNoAutoConfig() {
load(new Class[] {SampleBean.class}, "foo.name:test");
assertThat(this.context.getBean(SampleBean.class).getName(), is("default"));
}
private void load(Class<?>[] configs,
String... environment) {
this.context = new AnnotationConfigApplicationContext();
this.context.register(configs);
EnvironmentTestUtils.addEnvironment(this.context, environment);
this.context.refresh();
}
@Component
@ConfigurationProperties("foo")
private static class SampleBean {
private String name = "default";
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
}
}

View File

@ -630,9 +630,9 @@ Nested POJO properties can also be created (so a setter is not mandatory) if the
default constructor, or a constructor accepting a single value that can be coerced from default constructor, or a constructor accepting a single value that can be coerced from
String. Some people use Project Lombok to add getters and setters automatically. String. Some people use Project Lombok to add getters and setters automatically.
When the `@EnableConfigurationProperties` annotation is applied to your `@Configuration`, The `@EnableConfigurationProperties` annotation is automatically applied to your project
any beans annotated with `@ConfigurationProperties` will be automatically configured from so that any beans annotated with `@ConfigurationProperties` will be configured from the
the `Environment` properties. This style of configuration works particularly well with the `Environment` properties. This style of configuration works particularly well with the
`SpringApplication` external YAML configuration: `SpringApplication` external YAML configuration:
[source,yaml,indent=0] [source,yaml,indent=0]

View File

@ -20,14 +20,12 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner; import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Profile; import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.validation.Validator; import org.springframework.validation.Validator;
@SpringBootApplication @SpringBootApplication
@EnableConfigurationProperties
public class SamplePropertyValidationApplication { public class SamplePropertyValidationApplication {
@Bean @Bean