Skip to content

Commit 6890e65

Browse files
committed
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
1 parent 726a47d commit 6890e65

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

src/asciidoc/web-mvc.adoc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4676,6 +4676,18 @@ By default use of `@EnableWebMvc` or `<mvc:annotation-driven>` automatically reg
46764676
Validation support in Spring MVC through the `LocalValidatorFactoryBean` when a Bean Validation
46774677
provider is such as Hibernate Validator is detected on the classpath.
46784678

4679+
[NOTE]
4680+
====
4681+
Sometimes it's convenient to have a `LocalValidatorFactoryBean` injected into a controller
4682+
or another class. The easiest way to do that is to declare your own `@Bean` and also mark it
4683+
with `@Primary` in order to avoid a conflict with the one provided with the MVC Java config.
4684+
4685+
If you prefer to use the one from the MVC Java config, you'll need to override the
4686+
`mvcValidator` method from `WebMvcConfigurationSupport` and declare the method to explicitly
4687+
return `LocalValidatorFactory` rather than `Validator`. See <<mvc-config-advanced-java>>
4688+
for information on how to switch to extend the provided configuration.
4689+
====
4690+
46794691
Alternatively you can configure your own global `Validator` instance:
46804692

46814693
[source,java,indent=0]

0 commit comments

Comments
 (0)