scottfrederick/spring-framework — pull request #1 by scottfrederick (2012-02-08 04:00:25+00:00)
Title: Annotation-based Validation

This pull request is in response to this Jira issue: https://jira.springsource.org/browse/SPR-4774. It enables the kind of Validator class shown in the example in the last comment on the issue.

This code works great in a modified version of the PetClinic app I am using to test, by setting this AnnotationMethodValidator as the one system-level validator (i.e. by returning an object of this type from the getValidator() method of a config class that extends WebMvcConfigurerAdapter).

If an approach to validation like this one makes sense, then the next question is how to register this validation mechanism with the framework. In my opinion, registering both a LocalValidatorFactoryBean and an AnnotationMethodValidator by default would cover most validation needs in an application, and reduce the need to configure custom validators in ConfigurableWebBindingInitializers or @InitBinder methods.

Unfortunately, the concept of a single validator class goes pretty deep into the framework (through the Java Config extension points to the DataBinder). It seems like it would be much more flexible for an app to be able to register multiple validators, similar to just about every other extension point (formatters, message converters, argument resolvers, handler exception resolvers, view resolvers, resource handlers, etc...). I have started to make all the changes to support registering a set of validators and defaulting, but I would like to get guidance and advice that this is the right approach before continuing.