Add support for configuration properties scanning
See gh-12602
This commit is contained in:
@@ -1070,7 +1070,23 @@ missing property.
|
||||
[[boot-features-external-config-enabling]]
|
||||
==== Enabling `@ConfigurationProperties`-annotated types
|
||||
Spring Boot provides an infrastructure to bind such types and register them as beans
|
||||
automatically. Any `@Configuration` class can specify the list of types to process as
|
||||
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:
|
||||
|
||||
[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:
|
||||
|
||||
[source,java,indent=0]
|
||||
@@ -1083,13 +1099,13 @@ shown in the following example:
|
||||
|
||||
[NOTE]
|
||||
====
|
||||
When the `@ConfigurationProperties` bean is registered that way, 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. If the annotation does not provide any prefix, only the fully qualified
|
||||
name of the bean is used.
|
||||
When the `@ConfigurationProperties` bean is registered using 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. If the annotation does not
|
||||
provide any prefix, only the fully qualified name of the bean is used.
|
||||
|
||||
The bean name in the examples above is `acme-com.example.AcmeProperties`.
|
||||
The bean name in the example above is `acme-com.example.AcmeProperties`.
|
||||
====
|
||||
|
||||
We recommend that `@ConfigurationProperties` only deal with the environment and, in
|
||||
@@ -1100,27 +1116,10 @@ binder that only deals with the environment.
|
||||
For corner cases, setter injection can be used or any of the `*Aware` interfaces provided
|
||||
by the framework (such as `EnvironmentAware` if you need access to the `Environment`).
|
||||
|
||||
If you find using `@EnableConfigurationProperties` tedious, you can also declare a bean
|
||||
yourself. For instance, instead of annotating `MyConfiguration` with
|
||||
`@EnableConfigurationProperties(AcmeProperties.class)`, you could make `AcmeProperties`
|
||||
a bean, as shown in the following example:
|
||||
|
||||
[source,java,indent=0]
|
||||
----
|
||||
@Component
|
||||
@ConfigurationProperties(prefix="acme")
|
||||
public class AcmeProperties {
|
||||
|
||||
private boolean enabled;
|
||||
|
||||
private InetAddress remoteAddress;
|
||||
|
||||
private final Security security = new Security();
|
||||
|
||||
// ... see the preceding JavaBean properties binding example
|
||||
|
||||
}
|
||||
----
|
||||
NOTE: Annotating a `@ConfigurationProperties` type with `@Component` will result in two
|
||||
beans of the same type if the type is also scanned as part of classpath scanning. If you want
|
||||
to register the bean yourself using `@Component`, consider disabling scanning of
|
||||
`@ConfigurationProperties`.
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -336,8 +336,8 @@ best practices that help.
|
||||
When a class does not include a `package` declaration, it is considered to be in the
|
||||
"`default package`". The use of the "`default package`" is generally discouraged and
|
||||
should be avoided. It can cause particular problems for Spring Boot applications that use
|
||||
the `@ComponentScan`, `@EntityScan`, or `@SpringBootApplication` annotations, since every
|
||||
class from every jar is read.
|
||||
the `@ComponentScan`, `@ConfigurationPropertiesScan`, `@EntityScan`, or `@SpringBootApplication`
|
||||
annotations, since every class from every jar is read.
|
||||
|
||||
TIP: We recommend that you follow Java's recommended package naming conventions and use a
|
||||
reversed domain name (for example, `com.example.project`).
|
||||
@@ -355,8 +355,8 @@ is used to search for `@Entity` items. Using a root package also allows componen
|
||||
scan to apply only on your project.
|
||||
|
||||
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 that instead.
|
||||
`@ComponentScan`, and `@ConfigurationPropertiesScan` annotations that it imports defines
|
||||
that behaviour so you can also use those instead.
|
||||
|
||||
The following listing shows a typical layout:
|
||||
|
||||
@@ -556,12 +556,14 @@ be able to define extra configuration on their "application class". A single
|
||||
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
|
||||
|
||||
The `@SpringBootApplication` annotation is equivalent to using `@Configuration`,
|
||||
`@EnableAutoConfiguration`, and `@ComponentScan` with their default attributes, as shown
|
||||
in the following example:
|
||||
`@EnableAutoConfiguration`, @ComponentScan`, and `@ConfigurationPropertiesScan` with their default
|
||||
attributes, as shown in the following example:
|
||||
|
||||
|
||||
[source,java,indent=0]
|
||||
@@ -571,7 +573,7 @@ in the following example:
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication // same as @Configuration @EnableAutoConfiguration @ComponentScan
|
||||
@SpringBootApplication // same as @Configuration @EnableAutoConfiguration @ComponentScan @ConfigurationPropertiesScan
|
||||
public class Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
@@ -588,7 +590,7 @@ NOTE: `@SpringBootApplication` also provides aliases to customize the attributes
|
||||
====
|
||||
None of these features are mandatory and you may choose to replace this single annotation
|
||||
by any of the features that it enables. For instance, you may not want to use component
|
||||
scan in your application:
|
||||
scan or configuration properties scan in your application:
|
||||
|
||||
[source,java,indent=0]
|
||||
----
|
||||
@@ -612,8 +614,8 @@ scan in your application:
|
||||
----
|
||||
|
||||
In this example, `Application` is just like any other Spring Boot application except that
|
||||
`@Component`-annotated classes are not detected automatically and the user-defined beans
|
||||
are imported explicitly (see `@Import`).
|
||||
`@Component`-annotated classes and `@ConfigurationProperties`-annotated classes are not detected
|
||||
automatically and the user-defined beans are imported explicitly (see `@Import`).
|
||||
====
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user