提交 6890e65d 编写于 作者: R Rossen Stoyanchev

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
上级 726a47dd
......@@ -4676,6 +4676,18 @@ By default use of `@EnableWebMvc` or `<mvc:annotation-driven>` 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 <<mvc-config-advanced-java>>
for information on how to switch to extend the provided configuration.
====
Alternatively you can configure your own global `Validator` instance:
[source,java,indent=0]
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册