Use OptionalValidatorFactoryBean in Configurers

Configurers and BeanDefinitionParsers should use
OptionalValidatorFactoryBean instead of
LocalValidatorFactoryBean.

The Optional implementation catches and logs setup
exceptions, useful when a validation API is present on
the classpath but not the actual implementation.

Issue: SPR-11272
This commit is contained in:
Brian Clozel 2014-01-03 22:36:12 +01:00
parent 3bed6cfc7c
commit b9c8f47b01
4 changed files with 12 additions and 14 deletions

View File

@ -305,7 +305,7 @@ public abstract class AbstractMessageBrokerConfiguration implements ApplicationC
* <li>delegating to getValidator() first</li> * <li>delegating to getValidator() first</li>
* <li>if none returned, getting an existing instance with its well-known name "mvcValidator", created by an MVC configuration</li> * <li>if none returned, getting an existing instance with its well-known name "mvcValidator", created by an MVC configuration</li>
* <li>if none returned, checking the classpath for the presence of a JSR-303 implementation before creating a * <li>if none returned, checking the classpath for the presence of a JSR-303 implementation before creating a
* {@code LocalValidatorFactoryBean}</li> * {@code OptionalValidatorFactoryBean}</li>
* <li>returning a no-op Validator instance</li> * <li>returning a no-op Validator instance</li>
* </ul> * </ul>
*/ */
@ -318,7 +318,7 @@ public abstract class AbstractMessageBrokerConfiguration implements ApplicationC
else if (ClassUtils.isPresent("javax.validation.Validator", getClass().getClassLoader())) { else if (ClassUtils.isPresent("javax.validation.Validator", getClass().getClassLoader())) {
Class<?> clazz; Class<?> clazz;
try { try {
String className = "org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"; String className = "org.springframework.validation.beanvalidation.OptionalValidatorFactoryBean";
clazz = ClassUtils.forName(className, AbstractMessageBrokerConfiguration.class.getClassLoader()); clazz = ClassUtils.forName(className, AbstractMessageBrokerConfiguration.class.getClassLoader());
} }
catch (ClassNotFoundException e) { catch (ClassNotFoundException e) {

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2013 the original author or authors. * Copyright 2002-2014 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -48,7 +48,6 @@ import org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConvert
import org.springframework.http.converter.xml.SourceHttpMessageConverter; import org.springframework.http.converter.xml.SourceHttpMessageConverter;
import org.springframework.util.ClassUtils; import org.springframework.util.ClassUtils;
import org.springframework.util.xml.DomUtils; import org.springframework.util.xml.DomUtils;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
import org.springframework.web.HttpRequestHandler; import org.springframework.web.HttpRequestHandler;
import org.springframework.web.accept.ContentNegotiationManager; import org.springframework.web.accept.ContentNegotiationManager;
import org.springframework.web.accept.ContentNegotiationManagerFactoryBean; import org.springframework.web.accept.ContentNegotiationManagerFactoryBean;
@ -116,8 +115,8 @@ import org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolv
* <ul> * <ul>
* <li>A {@link ContentNegotiationManager} * <li>A {@link ContentNegotiationManager}
* <li>A {@link DefaultFormattingConversionService} * <li>A {@link DefaultFormattingConversionService}
* <li>A {@link LocalValidatorFactoryBean} if a JSR-303 implementation is * <li>A {@link org.springframework.validation.beanvalidation.LocalValidatorFactoryBean}
* available on the classpath * if a JSR-303 implementation is available on the classpath
* <li>A range of {@link HttpMessageConverter}s depending on what 3rd party * <li>A range of {@link HttpMessageConverter}s depending on what 3rd party
* libraries are available on the classpath. * libraries are available on the classpath.
* </ul> * </ul>

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2012 the original author or authors. * Copyright 2002-2014 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -52,7 +52,6 @@ import org.springframework.util.ClassUtils;
import org.springframework.validation.Errors; import org.springframework.validation.Errors;
import org.springframework.validation.MessageCodesResolver; import org.springframework.validation.MessageCodesResolver;
import org.springframework.validation.Validator; import org.springframework.validation.Validator;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
import org.springframework.web.HttpRequestHandler; import org.springframework.web.HttpRequestHandler;
import org.springframework.web.accept.ContentNegotiationManager; import org.springframework.web.accept.ContentNegotiationManager;
import org.springframework.web.bind.WebDataBinder; import org.springframework.web.bind.WebDataBinder;
@ -129,8 +128,8 @@ import org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolv
* <ul> * <ul>
* <li>A {@link ContentNegotiationManager} * <li>A {@link ContentNegotiationManager}
* <li>A {@link DefaultFormattingConversionService} * <li>A {@link DefaultFormattingConversionService}
* <li>A {@link LocalValidatorFactoryBean} if a JSR-303 implementation is * <li>A {@link org.springframework.validation.beanvalidation.OptionalValidatorFactoryBean}
* available on the classpath * if a JSR-303 implementation is available on the classpath
* <li>A range of {@link HttpMessageConverter}s depending on the 3rd party * <li>A range of {@link HttpMessageConverter}s depending on the 3rd party
* libraries available on the classpath. * libraries available on the classpath.
* </ul> * </ul>
@ -410,7 +409,7 @@ public class WebMvcConfigurationSupport implements ApplicationContextAware, Serv
* {@code @ModelAttribute} and {@code @RequestBody} method arguments. * {@code @ModelAttribute} and {@code @RequestBody} method arguments.
* Delegates to {@link #getValidator()} first and if that returns {@code null} * Delegates to {@link #getValidator()} first and if that returns {@code null}
* checks the classpath for the presence of a JSR-303 implementations * checks the classpath for the presence of a JSR-303 implementations
* before creating a {@code LocalValidatorFactoryBean}.If a JSR-303 * before creating a {@code OptionalValidatorFactoryBean}.If a JSR-303
* implementation is not available, a no-op {@link Validator} is returned. * implementation is not available, a no-op {@link Validator} is returned.
*/ */
@Bean @Bean
@ -420,7 +419,7 @@ public class WebMvcConfigurationSupport implements ApplicationContextAware, Serv
if (ClassUtils.isPresent("javax.validation.Validator", getClass().getClassLoader())) { if (ClassUtils.isPresent("javax.validation.Validator", getClass().getClassLoader())) {
Class<?> clazz; Class<?> clazz;
try { try {
String className = "org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"; String className = "org.springframework.validation.beanvalidation.OptionalValidatorFactoryBean";
clazz = ClassUtils.forName(className, WebMvcConfigurationSupport.class.getClassLoader()); clazz = ClassUtils.forName(className, WebMvcConfigurationSupport.class.getClassLoader());
} }
catch (ClassNotFoundException e) { catch (ClassNotFoundException e) {

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2012 the original author or authors. * Copyright 2002-2014 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -64,7 +64,7 @@ public interface WebMvcConfigurer {
/** /**
* Provide a custom {@link Validator} instead of the one created by default. * Provide a custom {@link Validator} instead of the one created by default.
* The default implementation, assuming JSR-303 is on the classpath, is: * The default implementation, assuming JSR-303 is on the classpath, is:
* {@link org.springframework.validation.beanvalidation.LocalValidatorFactoryBean}. * {@link org.springframework.validation.beanvalidation.OptionalValidatorFactoryBean}.
* Leave the return value as {@code null} to keep the default. * Leave the return value as {@code null} to keep the default.
*/ */
Validator getValidator(); Validator getValidator();