Skip to content

Commit e26d5d9

Browse files
committed
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
1 parent e9f231e commit e26d5d9

File tree

4 files changed

+21
-18
lines changed

4 files changed

+21
-18
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/SpringBootApplication.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@
4040
* auto-configuration}, {@link ComponentScan component scanning}, and
4141
* {@link ConfigurationPropertiesScan configuration properties scanning}. This is a
4242
* convenience annotation that is equivalent to declaring {@code @Configuration},
43-
* {@code @EnableAutoConfiguration}, {@code @ComponentScan}, and
44-
* {@code @ConfigurationPropertiesScan}.
43+
* {@code @EnableAutoConfiguration}, {@code @ComponentScan}.
4544
*
4645
* @author Phillip Webb
4746
* @author Stephane Nicoll
@@ -56,7 +55,6 @@
5655
@EnableAutoConfiguration
5756
@ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
5857
@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
59-
@ConfigurationPropertiesScan
6058
public @interface SpringBootApplication {
6159

6260
/**

spring-boot-project/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -917,27 +917,31 @@ TIP: If you have more than one constructor for your class you can also use `@Con
917917

918918
[[boot-features-external-config-enabling]]
919919
==== Enabling `@ConfigurationProperties`-annotated types
920-
Spring Boot provides an infrastructure to bind such types and register them as beans automatically.
921-
If your application uses `@SpringBootApplication`, classes annotated with `@ConfigurationProperties` will automatically be scanned and registered as beans.
922-
By default, scanning will occur from the package of the class that declares this annotation.
923-
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:
920+
Spring Boot provides infrastructure to bind `@ConfigurationProperties` types and register them as beans.
921+
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.
922+
923+
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.
924+
In these cases, specify the list of types to process using the `@EnableConfigurationProperties` annotation.
925+
This can be done on any `@Configuration` class, as shown in the following example:
924926

925927
[source,java,indent=0]
926928
----
927-
@SpringBootApplication
928-
@ConfigurationPropertiesScan({ "com.example.app", "org.acme.another" })
929-
public class MyApplication {
929+
@Configuration(proxyBeanMethods = false)
930+
@EnableConfigurationProperties(AcmeProperties.class)
931+
public class MyConfiguration {
930932
}
931933
----
932934

933-
Sometimes, classes annotated with `@ConfigurationProperties` might not be suitable for scanning, for example, if you're developing your own auto-configuration.
934-
In these cases, you can specify the list of types to process on any `@Configuration` class as shown in the following example:
935+
To use configuration property scanning, add the `@ConfigurationPropertiesScan` annotation to your application.
936+
Typically, it is added to the main application class that is annotated with `@SpringBootApplication` but it can be added to any `@Configuration` class.
937+
By default, scanning will occur from the package of the class that declares the annotation.
938+
If you want to define specific packages to scan, you can do so as shown in the following example:
935939

936940
[source,java,indent=0]
937941
----
938-
@Configuration(proxyBeanMethods = false)
939-
@EnableConfigurationProperties(AcmeProperties.class)
940-
public class MyConfiguration {
942+
@SpringBootApplication
943+
@ConfigurationPropertiesScan({ "com.example.app", "org.acme.another" })
944+
public class MyApplication {
941945
}
942946
----
943947

spring-boot-project/spring-boot-docs/src/main/asciidoc/using-spring-boot.adoc

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ The <<using-boot-using-springbootapplication-annotation, `@SpringBootApplication
298298
For example, if you are writing a JPA application, the package of the `@SpringBootApplication` annotated class is used to search for `@Entity` items.
299299
Using a root package also allows component scan to apply only on your project.
300300

301-
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.
301+
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.
302302

303303
The following listing shows a typical layout:
304304

@@ -480,7 +480,6 @@ A single `@SpringBootApplication` annotation can be used to enable those three f
480480

481481
* `@EnableAutoConfiguration`: enable <<using-boot-auto-configuration,Spring Boot's auto-configuration mechanism>>
482482
* `@ComponentScan`: enable `@Component` scan on the package where the application is located (see <<using-boot-structuring-your-code,the best practices>>)
483-
* `@ConfigurationPropertiesScan`: enable `@ConfigurationProperties` scan on the package where the application is located (see <<using-boot-structuring-your-code,the best practices>>)
484483
* `@Configuration`: allow to register extra beans in the context or import additional configuration classes
485484

486485
[source,java,indent=0]
@@ -490,7 +489,7 @@ A single `@SpringBootApplication` annotation can be used to enable those three f
490489
import org.springframework.boot.SpringApplication;
491490
import org.springframework.boot.autoconfigure.SpringBootApplication;
492491
493-
@SpringBootApplication // same as @Configuration @EnableAutoConfiguration @ComponentScan @ConfigurationPropertiesScan
492+
@SpringBootApplication // same as @Configuration @EnableAutoConfiguration @ComponentScan
494493
public class Application {
495494
496495
public static void main(String[] args) {

spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/client/ExampleWebClientApplication.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.boot.test.autoconfigure.web.client;
1818

1919
import org.springframework.boot.autoconfigure.SpringBootApplication;
20+
import org.springframework.boot.context.properties.ConfigurationPropertiesScan;
2021

2122
/**
2223
* Example {@link SpringBootApplication @SpringBootApplication} used with
@@ -25,6 +26,7 @@
2526
* @author Phillip Webb
2627
*/
2728
@SpringBootApplication
29+
@ConfigurationPropertiesScan
2830
public class ExampleWebClientApplication {
2931

3032
}

0 commit comments

Comments
 (0)