Require annotation signal for constructor binding
Update `@ConfigurationProperties` constructor binding support to only apply when a `@ConstructorBinding` annotation is present on either the type or the specific constructor to use. Prior to this commit we didn't have a good way to tell when constructor binding should be used vs regular autowiring. For convenience, an `@ImmutableConfigurationProperties` meta-annotation has also been added which is composed of `@ConfigurationProperties` and `@ConstructorBinding`. Closes gh-18469
This commit is contained in:
@@ -852,7 +852,7 @@ The example in the previous section can be rewritten in an immutable fashion as
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.boot.context.properties.DefaultValue;
|
||||
|
||||
@ConfigurationProperties("acme")
|
||||
@ImmutableConfigurationProperties("acme")
|
||||
public class AcmeProperties {
|
||||
|
||||
private final boolean enabled;
|
||||
@@ -881,6 +881,7 @@ The example in the previous section can be rewritten in an immutable fashion as
|
||||
|
||||
private final List<String> roles;
|
||||
|
||||
@ConstructorBinding
|
||||
public Security(String username, String password,
|
||||
@DefaultValue("USER") List<String> roles) {
|
||||
this.username = username;
|
||||
@@ -899,11 +900,18 @@ The example in the previous section can be rewritten in an immutable fashion as
|
||||
}
|
||||
----
|
||||
|
||||
In this setup one, and only one constructor must be defined with the list of properties that you wish to bind and not other properties than the ones in the constructor are bound.
|
||||
In this setup, the `@ImmutableConfigurationProperties` annotation is used to indicate that constructor binding should be used.
|
||||
This means that the binder will expect to find a constructor with the parameters that you wish to have bound bind.
|
||||
|
||||
Nested classes that also require constructor binding (such as `Security` in the example above) should use the `@ConstructorBinding` annotation.
|
||||
|
||||
Default values can be specified using `@DefaultValue` and the same conversion service will be applied to coerce the `String` value to the target type of a missing property.
|
||||
|
||||
NOTE: To use constructor binding the class must not be annotated with `@Component` and must be enabled using `@EnableConfigurationProperties` or configuration property scanning instead.
|
||||
TIP: You can also use `@ConstructorBinding` on the actual constructor that should be bound.
|
||||
This is required if you have more than one constructor for your class.
|
||||
|
||||
NOTE: To use constructor binding the class must be enabled using `@EnableConfigurationProperties` or configuration property scanning.
|
||||
You cannot use constructor binding with beans that are created by the regular Spring mechanisms (e.g. `@Component` beans, beans created via `@Bean` methods or beans loaded using `@Import`)
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user