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
This commit is contained in:
parent
e9f231e8df
commit
e26d5d95a8
|
|
@ -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 {
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -917,21 +917,12 @@ 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.
|
||||
|
||||
[source,java,indent=0]
|
||||
----
|
||||
@SpringBootApplication
|
||||
@ConfigurationPropertiesScan({ "com.example.app", "org.acme.another" })
|
||||
public class MyApplication {
|
||||
}
|
||||
----
|
||||
|
||||
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:
|
||||
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]
|
||||
----
|
||||
|
|
@ -941,6 +932,19 @@ In these cases, you can specify the list of types to process on any `@Configurat
|
|||
}
|
||||
----
|
||||
|
||||
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]
|
||||
----
|
||||
@SpringBootApplication
|
||||
@ConfigurationPropertiesScan({ "com.example.app", "org.acme.another" })
|
||||
public class MyApplication {
|
||||
}
|
||||
----
|
||||
|
||||
[NOTE]
|
||||
====
|
||||
When the `@ConfigurationProperties` bean is registered using configuration property scanning or via `@EnableConfigurationProperties`, the bean has a conventional name: `<prefix>-<fqn>`, where `<prefix>` is the environment key prefix specified in the `@ConfigurationProperties` annotation and `<fqn>` is the fully qualified name of the bean.
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
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:
|
||||
|
||||
|
|
@ -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>>
|
||||
* `@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
|
||||
|
||||
[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) {
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue