Do not enable @ConfigurationPropertiesScan be default

In 2.2.0, @ConfigurationPropertiesScan was enabled by default.
Unfortunately, this had the unexpected side-effect of breaking
conditional enablement of a @ConfigurationProperties class via
@EnableConfigurationProperties if the @ConfigurationProperties class
was in a package covered by scanning.

This commit remove @ConfigurationPropertiesScan from
@SpringBootApplication so that it is no longer enabled by default.
2.1.x users who rely upon such conditional enablement of
@ConfigurationProperties classes can now upgrade to 2.2.x without
having to make any changes. Users who do not have such a need and are
in a position to use configuration properties scanning can now opt-in
by adding @ConfigurationPropertiesScan to their main application class
alongside @SpringBootApplication.

Closes gh-18674
pull/18854/head
Andy Wilkinson 5 years ago
parent e9f231e8df
commit e26d5d95a8

@ -40,8 +40,7 @@ import org.springframework.data.repository.Repository;
* auto-configuration}, {@link ComponentScan component scanning}, and * auto-configuration}, {@link ComponentScan component scanning}, and
* {@link ConfigurationPropertiesScan configuration properties scanning}. This is a * {@link ConfigurationPropertiesScan configuration properties scanning}. This is a
* convenience annotation that is equivalent to declaring {@code @Configuration}, * convenience annotation that is equivalent to declaring {@code @Configuration},
* {@code @EnableAutoConfiguration}, {@code @ComponentScan}, and * {@code @EnableAutoConfiguration}, {@code @ComponentScan}.
* {@code @ConfigurationPropertiesScan}.
* *
* @author Phillip Webb * @author Phillip Webb
* @author Stephane Nicoll * @author Stephane Nicoll
@ -56,7 +55,6 @@ import org.springframework.data.repository.Repository;
@EnableAutoConfiguration @EnableAutoConfiguration
@ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class), @ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) }) @Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
@ConfigurationPropertiesScan
public @interface SpringBootApplication { public @interface SpringBootApplication {
/** /**

@ -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]] [[boot-features-external-config-enabling]]
==== Enabling `@ConfigurationProperties`-annotated types ==== Enabling `@ConfigurationProperties`-annotated types
Spring Boot provides an infrastructure to bind such types and register them as beans automatically. Spring Boot provides infrastructure to bind `@ConfigurationProperties` types and register them as beans.
If your application uses `@SpringBootApplication`, classes annotated with `@ConfigurationProperties` will automatically be scanned and registered 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.
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: 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] [source,java,indent=0]
---- ----
@SpringBootApplication @Configuration(proxyBeanMethods = false)
@ConfigurationPropertiesScan({ "com.example.app", "org.acme.another" }) @EnableConfigurationProperties(AcmeProperties.class)
public class MyApplication { public class MyConfiguration {
} }
---- ----
Sometimes, classes annotated with `@ConfigurationProperties` might not be suitable for scanning, for example, if you're developing your own auto-configuration. To use configuration property scanning, add the `@ConfigurationPropertiesScan` annotation to your application.
In these cases, you can specify the list of types to process on any `@Configuration` class as shown in the following example: 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] [source,java,indent=0]
---- ----
@Configuration(proxyBeanMethods = false) @SpringBootApplication
@EnableConfigurationProperties(AcmeProperties.class) @ConfigurationPropertiesScan({ "com.example.app", "org.acme.another" })
public class MyConfiguration { public class MyApplication {
} }
---- ----

@ -298,7 +298,7 @@ The <<using-boot-using-springbootapplication-annotation, `@SpringBootApplication
For example, if you are writing a JPA application, the package of the `@SpringBootApplication` annotated class is used to search for `@Entity` items. For example, if you are writing a JPA application, the package of the `@SpringBootApplication` annotated class is used to search for `@Entity` items.
Using a root package also allows component scan to apply only on your project. Using a root package also allows component scan to apply only on your project.
TIP: If you don't want to use `@SpringBootApplication`, the `@EnableAutoConfiguration` `@ComponentScan`, and `@ConfigurationPropertiesScan` annotations that it imports defines that behaviour so you can also use those instead. TIP: If you don't want to use `@SpringBootApplication`, the `@EnableAutoConfiguration` and `@ComponentScan` annotations that it imports defines that behaviour so you can also use those instead.
The following listing shows a typical layout: The following listing shows a typical layout:
@ -480,7 +480,6 @@ A single `@SpringBootApplication` annotation can be used to enable those three f
* `@EnableAutoConfiguration`: enable <<using-boot-auto-configuration,Spring Boot's auto-configuration mechanism>> * `@EnableAutoConfiguration`: enable <<using-boot-auto-configuration,Spring Boot's auto-configuration mechanism>>
* `@ComponentScan`: enable `@Component` scan on the package where the application is located (see <<using-boot-structuring-your-code,the best practices>>) * `@ComponentScan`: enable `@Component` scan on the package where the application is located (see <<using-boot-structuring-your-code,the best practices>>)
* `@ConfigurationPropertiesScan`: enable `@ConfigurationProperties` scan on the package where the application is located (see <<using-boot-structuring-your-code,the best practices>>)
* `@Configuration`: allow to register extra beans in the context or import additional configuration classes * `@Configuration`: allow to register extra beans in the context or import additional configuration classes
[source,java,indent=0] [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.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication // same as @Configuration @EnableAutoConfiguration @ComponentScan @ConfigurationPropertiesScan @SpringBootApplication // same as @Configuration @EnableAutoConfiguration @ComponentScan
public class Application { public class Application {
public static void main(String[] args) { public static void main(String[] args) {

@ -17,6 +17,7 @@
package org.springframework.boot.test.autoconfigure.web.client; package org.springframework.boot.test.autoconfigure.web.client;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationPropertiesScan;
/** /**
* Example {@link SpringBootApplication @SpringBootApplication} used with * Example {@link SpringBootApplication @SpringBootApplication} used with
@ -25,6 +26,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
* @author Phillip Webb * @author Phillip Webb
*/ */
@SpringBootApplication @SpringBootApplication
@ConfigurationPropertiesScan
public class ExampleWebClientApplication { public class ExampleWebClientApplication {
} }

Loading…
Cancel
Save