From b9c8f47b01b0bb2833539643d429b79ef0adabf5 Mon Sep 17 00:00:00 2001 From: Brian Clozel Date: Fri, 3 Jan 2014 22:36:12 +0100 Subject: [PATCH] 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 --- .../config/AbstractMessageBrokerConfiguration.java | 4 ++-- .../config/AnnotationDrivenBeanDefinitionParser.java | 7 +++---- .../config/annotation/WebMvcConfigurationSupport.java | 11 +++++------ .../servlet/config/annotation/WebMvcConfigurer.java | 4 ++-- 4 files changed, 12 insertions(+), 14 deletions(-) diff --git a/spring-messaging/src/main/java/org/springframework/messaging/simp/config/AbstractMessageBrokerConfiguration.java b/spring-messaging/src/main/java/org/springframework/messaging/simp/config/AbstractMessageBrokerConfiguration.java index aa94adec83c..53e4a9537dd 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/simp/config/AbstractMessageBrokerConfiguration.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/simp/config/AbstractMessageBrokerConfiguration.java @@ -305,7 +305,7 @@ public abstract class AbstractMessageBrokerConfiguration implements ApplicationC *
  • delegating to getValidator() first
  • *
  • if none returned, getting an existing instance with its well-known name "mvcValidator", created by an MVC configuration
  • *
  • if none returned, checking the classpath for the presence of a JSR-303 implementation before creating a - * {@code LocalValidatorFactoryBean}
  • + * {@code OptionalValidatorFactoryBean} *
  • returning a no-op Validator instance
  • * */ @@ -318,7 +318,7 @@ public abstract class AbstractMessageBrokerConfiguration implements ApplicationC else if (ClassUtils.isPresent("javax.validation.Validator", getClass().getClassLoader())) { Class clazz; try { - String className = "org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"; + String className = "org.springframework.validation.beanvalidation.OptionalValidatorFactoryBean"; clazz = ClassUtils.forName(className, AbstractMessageBrokerConfiguration.class.getClassLoader()); } catch (ClassNotFoundException e) { diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/AnnotationDrivenBeanDefinitionParser.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/AnnotationDrivenBeanDefinitionParser.java index c009f0badfd..d3d345a1e57 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/AnnotationDrivenBeanDefinitionParser.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/AnnotationDrivenBeanDefinitionParser.java @@ -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"); * 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.util.ClassUtils; import org.springframework.util.xml.DomUtils; -import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean; import org.springframework.web.HttpRequestHandler; import org.springframework.web.accept.ContentNegotiationManager; import org.springframework.web.accept.ContentNegotiationManagerFactoryBean; @@ -116,8 +115,8 @@ import org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolv * diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupport.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupport.java index 7737ea20639..ccef1bef12a 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupport.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupport.java @@ -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"); * 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.MessageCodesResolver; import org.springframework.validation.Validator; -import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean; import org.springframework.web.HttpRequestHandler; import org.springframework.web.accept.ContentNegotiationManager; import org.springframework.web.bind.WebDataBinder; @@ -129,8 +128,8 @@ import org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolv * @@ -410,7 +409,7 @@ public class WebMvcConfigurationSupport implements ApplicationContextAware, Serv * {@code @ModelAttribute} and {@code @RequestBody} method arguments. * Delegates to {@link #getValidator()} first and if that returns {@code null} * 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. */ @Bean @@ -420,7 +419,7 @@ public class WebMvcConfigurationSupport implements ApplicationContextAware, Serv if (ClassUtils.isPresent("javax.validation.Validator", getClass().getClassLoader())) { Class clazz; try { - String className = "org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"; + String className = "org.springframework.validation.beanvalidation.OptionalValidatorFactoryBean"; clazz = ClassUtils.forName(className, WebMvcConfigurationSupport.class.getClassLoader()); } catch (ClassNotFoundException e) { diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurer.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurer.java index 215d34b7da7..c7f0a6fc9d9 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurer.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurer.java @@ -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"); * 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. * 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. */ Validator getValidator();