From 6890e65d2cd98ad89e08e2afbbb1213241a791ac Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Tue, 7 Jul 2015 12:33:32 -0400 Subject: [PATCH] Suggest use of @Primary for JSR-303 with Spring MVC A note is added to suggest the use of @Primary with a LocalValidatorFactory bean next to the MVC Java config. Issue: SPR-12194 --- src/asciidoc/web-mvc.adoc | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/asciidoc/web-mvc.adoc b/src/asciidoc/web-mvc.adoc index 54b495982e..13296ff925 100644 --- a/src/asciidoc/web-mvc.adoc +++ b/src/asciidoc/web-mvc.adoc @@ -4676,6 +4676,18 @@ By default use of `@EnableWebMvc` or `` automatically reg Validation support in Spring MVC through the `LocalValidatorFactoryBean` when a Bean Validation provider is such as Hibernate Validator is detected on the classpath. +[NOTE] +==== +Sometimes it's convenient to have a `LocalValidatorFactoryBean` injected into a controller +or another class. The easiest way to do that is to declare your own `@Bean` and also mark it +with `@Primary` in order to avoid a conflict with the one provided with the MVC Java config. + +If you prefer to use the one from the MVC Java config, you'll need to override the +`mvcValidator` method from `WebMvcConfigurationSupport` and declare the method to explicitly +return `LocalValidatorFactory` rather than `Validator`. See <> +for information on how to switch to extend the provided configuration. +==== + Alternatively you can configure your own global `Validator` instance: [source,java,indent=0]