Validate @ConfigurationProperties on @Bean methods

Refactor `ConfigurationPropertiesBindingPostProcessor` to allow JSR-303
validation on `@ConfigurationProperties` defined at the `@Bean` method
level.

JSR-303 validation is now applied when a JSR-303 implementation is
available and `@Validated` is present on either the configuration
properties class itself or the `@Bean` method that creates it.

Standard Spring validation is also supported using a validator bean
named `configurationPropertiesValidator`, or by having the configuration
properties implement `Validator`.

The commit also consolidates tests into a single location.

Fixes gh-10803
This commit is contained in:
Phillip Webb
2018-02-14 17:36:55 -08:00
parent 9e75680e6f
commit 1d83e87bcd
24 changed files with 2132 additions and 2639 deletions

View File

@@ -1213,9 +1213,13 @@ to your fields, as shown in the following example:
}
----
In order to validate the values of nested properties, you must annotate the associated
field as `@Valid` to trigger its validation. The following example builds on the
preceding `AcmeProperties` example:
TIP: You can also trigger validation by annotating the `@Bean` method that creates the
configuration properties with `@Validated`.
Although nested properties will also be validated when bound, it's good practice to
also annotate the associated field as `@Valid`. This ensure that validation is triggered
even if no nested properties are found. The following example builds on the preceding
`AcmeProperties` example:
[source,java,indent=0]
----