From 7cbc115da6fdf463ce5f9daf821f0321a6cba781 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Fri, 3 May 2013 15:19:28 +0200 Subject: [PATCH] Added "postProcessConfiguration" template method to LocalValidatorFactoryBean; clarified limited compatibility with Hibernate Validator 5.0 --- .../LocalValidatorFactoryBean.java | 23 +++++++++++++++++-- .../MethodValidationPostProcessor.java | 9 ++++---- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/spring-context/src/main/java/org/springframework/validation/beanvalidation/LocalValidatorFactoryBean.java b/spring-context/src/main/java/org/springframework/validation/beanvalidation/LocalValidatorFactoryBean.java index 1cc392278f..a8ab96df7e 100644 --- a/spring-context/src/main/java/org/springframework/validation/beanvalidation/LocalValidatorFactoryBean.java +++ b/spring-context/src/main/java/org/springframework/validation/beanvalidation/LocalValidatorFactoryBean.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2013 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. @@ -51,6 +51,12 @@ import org.springframework.util.CollectionUtils; * you will almost always use the default Validator anyway. This can also be injected directly * into any target dependency of type {@link org.springframework.validation.Validator}! * + *

NOTE: This class is based on Bean Validation 1.0 and (optionally) Hibernate Validator 4.x. + * Nevertheless, its core functionality does work against Bean Validation 1.1 at runtime. + * However, there is no special support for Hibernate Validator 5.0 yet; as a consequence, + * {@link #setValidationMessageSource} won't work in that scenario. Please stick with + * Hibernate Validator 4.3 for the time being, or upgrade to Spring Framework 4.0. + * * @author Juergen Hoeller * @since 3.0 * @see javax.validation.ValidatorFactory @@ -217,10 +223,23 @@ public class LocalValidatorFactoryBean extends SpringValidatorAdapter configuration.addProperty(entry.getKey(), entry.getValue()); } + // Allow for custom post-processing before we actually build the ValidatorFactory. + postProcessConfiguration(configuration); + this.validatorFactory = configuration.buildValidatorFactory(); setTargetValidator(this.validatorFactory.getValidator()); } + /** + * Post-process the given Bean Validation configuration, + * adding to or overriding any of its settings. + *

Invoked right before building the {@link ValidatorFactory}. + * @param configuration the Configuration object, pre-populated with + * settings driven by LocalValidatorFactoryBean's properties + */ + protected void postProcessConfiguration(Configuration configuration) { + } + public Validator getValidator() { return this.validatorFactory.getValidator(); @@ -244,7 +263,7 @@ public class LocalValidatorFactoryBean extends SpringValidatorAdapter /** - * Inner class to avoid a hard-coded Hibernate Validator 4.1 dependency. + * Inner class to avoid a hard-coded Hibernate Validator 4.1+ dependency. */ private static class HibernateValidatorDelegate { diff --git a/spring-context/src/main/java/org/springframework/validation/beanvalidation/MethodValidationPostProcessor.java b/spring-context/src/main/java/org/springframework/validation/beanvalidation/MethodValidationPostProcessor.java index c8d909bd42..cb7d6536b7 100644 --- a/spring-context/src/main/java/org/springframework/validation/beanvalidation/MethodValidationPostProcessor.java +++ b/spring-context/src/main/java/org/springframework/validation/beanvalidation/MethodValidationPostProcessor.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2013 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,9 +48,10 @@ import org.springframework.validation.annotation.Validated; * inline constraint annotations. Validation groups can be specified through {@code @Validated} * as well. By default, JSR-303 will validate against its default group only. * - *

As of Spring 3.1, this functionality requires Hibernate Validator 4.2 or higher. - * In a future version of Spring, this class will autodetect a Bean Validation 1.1 compliant - * provider and automatically use the standard method validation support when available. + *

As of Spring 3.1, this functionality requires Hibernate Validator 4.2 or 4.3. + * In Spring 4.0, this class will autodetect a Bean Validation 1.1 compliant provider + * (such as Hibernate Validator 5.0) and automatically use the standard method + * validation support when available. * * @author Juergen Hoeller * @since 3.1