diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/SpringBootApplication.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/SpringBootApplication.java index b06720a1fe..b341032d68 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/SpringBootApplication.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/SpringBootApplication.java @@ -40,8 +40,7 @@ import org.springframework.data.repository.Repository; * auto-configuration}, {@link ComponentScan component scanning}, and * {@link ConfigurationPropertiesScan configuration properties scanning}. This is a * convenience annotation that is equivalent to declaring {@code @Configuration}, - * {@code @EnableAutoConfiguration}, {@code @ComponentScan}, and - * {@code @ConfigurationPropertiesScan}. + * {@code @EnableAutoConfiguration}, {@code @ComponentScan}. * * @author Phillip Webb * @author Stephane Nicoll @@ -56,7 +55,6 @@ import org.springframework.data.repository.Repository; @EnableAutoConfiguration @ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class), @Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) }) -@ConfigurationPropertiesScan public @interface SpringBootApplication { /** diff --git a/spring-boot-project/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc b/spring-boot-project/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc index d4ccec5768..61b96dc910 100644 --- a/spring-boot-project/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc +++ b/spring-boot-project/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc @@ -917,27 +917,31 @@ TIP: If you have more than one constructor for your class you can also use `@Con [[boot-features-external-config-enabling]] ==== Enabling `@ConfigurationProperties`-annotated types -Spring Boot provides an infrastructure to bind such types and register them as beans automatically. -If your application uses `@SpringBootApplication`, classes annotated with `@ConfigurationProperties` will automatically be scanned and registered as beans. -By default, scanning will occur from the package of the class that declares this annotation. -If you want to define specific packages to scan, you can do so using an explicit `@ConfigurationPropertiesScan` directive on your `@SpringBootApplication`-annotated class as shown in the following example: +Spring Boot provides infrastructure to bind `@ConfigurationProperties` types and register them as beans. +You can either enable configuration properties on a class-by-class basis or enable configuration property scanning that works in a similar manner to component scanning. + +Sometimes, classes annotated with `@ConfigurationProperties` might not be suitable for scanning, for example, if you're developing your own auto-configuration or you want to enable them conditionally. +In these cases, specify the list of types to process using the `@EnableConfigurationProperties` annotation. +This can be done on any `@Configuration` class, as shown in the following example: [source,java,indent=0] ---- - @SpringBootApplication - @ConfigurationPropertiesScan({ "com.example.app", "org.acme.another" }) - public class MyApplication { + @Configuration(proxyBeanMethods = false) + @EnableConfigurationProperties(AcmeProperties.class) + public class MyConfiguration { } ---- -Sometimes, classes annotated with `@ConfigurationProperties` might not be suitable for scanning, for example, if you're developing your own auto-configuration. -In these cases, you can specify the list of types to process on any `@Configuration` class as shown in the following example: +To use configuration property scanning, add the `@ConfigurationPropertiesScan` annotation to your application. +Typically, it is added to the main application class that is annotated with `@SpringBootApplication` but it can be added to any `@Configuration` class. +By default, scanning will occur from the package of the class that declares the annotation. +If you want to define specific packages to scan, you can do so as shown in the following example: [source,java,indent=0] ---- - @Configuration(proxyBeanMethods = false) - @EnableConfigurationProperties(AcmeProperties.class) - public class MyConfiguration { + @SpringBootApplication + @ConfigurationPropertiesScan({ "com.example.app", "org.acme.another" }) + public class MyApplication { } ---- diff --git a/spring-boot-project/spring-boot-docs/src/main/asciidoc/using-spring-boot.adoc b/spring-boot-project/spring-boot-docs/src/main/asciidoc/using-spring-boot.adoc index 6dd8a8ae72..3eb1af6419 100644 --- a/spring-boot-project/spring-boot-docs/src/main/asciidoc/using-spring-boot.adoc +++ b/spring-boot-project/spring-boot-docs/src/main/asciidoc/using-spring-boot.adoc @@ -298,7 +298,7 @@ The <> * `@ComponentScan`: enable `@Component` scan on the package where the application is located (see <>) -* `@ConfigurationPropertiesScan`: enable `@ConfigurationProperties` scan on the package where the application is located (see <>) * `@Configuration`: allow to register extra beans in the context or import additional configuration classes [source,java,indent=0] @@ -490,7 +489,7 @@ A single `@SpringBootApplication` annotation can be used to enable those three f import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; - @SpringBootApplication // same as @Configuration @EnableAutoConfiguration @ComponentScan @ConfigurationPropertiesScan + @SpringBootApplication // same as @Configuration @EnableAutoConfiguration @ComponentScan public class Application { public static void main(String[] args) { diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/client/ExampleWebClientApplication.java b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/client/ExampleWebClientApplication.java index 3a7ebc3620..bb3cac6a36 100644 --- a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/client/ExampleWebClientApplication.java +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/client/ExampleWebClientApplication.java @@ -17,6 +17,7 @@ package org.springframework.boot.test.autoconfigure.web.client; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.context.properties.ConfigurationPropertiesScan; /** * Example {@link SpringBootApplication @SpringBootApplication} used with @@ -25,6 +26,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; * @author Phillip Webb */ @SpringBootApplication +@ConfigurationPropertiesScan public class ExampleWebClientApplication { }