diff --git a/build.gradle b/build.gradle index 8451b38356..ab7ab92c62 100644 --- a/build.gradle +++ b/build.gradle @@ -701,6 +701,7 @@ project("spring-web") { optional("javax.portlet:portlet-api:2.0") optional("javax.el:javax.el-api:2.2.5") optional("javax.faces:javax.faces-api:2.2") + optional("javax.validation:validation-api:1.0.0.GA") optional("aopalliance:aopalliance:1.0") optional("org.codehaus.groovy:groovy-all:${groovyVersion}") optional("com.caucho:hessian:4.0.38") diff --git a/spring-context/src/main/java/org/springframework/validation/beanvalidation/SpringConstraintValidatorFactory.java b/spring-context/src/main/java/org/springframework/validation/beanvalidation/SpringConstraintValidatorFactory.java index c6394d76b1..a2c718a7a9 100644 --- a/spring-context/src/main/java/org/springframework/validation/beanvalidation/SpringConstraintValidatorFactory.java +++ b/spring-context/src/main/java/org/springframework/validation/beanvalidation/SpringConstraintValidatorFactory.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2015 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. @@ -26,6 +26,11 @@ import org.springframework.util.Assert; * JSR-303 {@link ConstraintValidatorFactory} implementation that delegates to a * Spring BeanFactory for creating autowired {@link ConstraintValidator} instances. * + *

Note that this class is meant for programmatic use, not for declarative use + * in a standard {@code validation.xml} file. Consider + * {@link org.springframework.web.bind.support.SpringWebConstraintValidatorFactory} + * for declarative use in a web application, e.g. with JAX-RS or JAX-WS. + * * @author Juergen Hoeller * @since 3.0 * @see org.springframework.beans.factory.config.AutowireCapableBeanFactory#createBean(Class) @@ -51,6 +56,7 @@ public class SpringConstraintValidatorFactory implements ConstraintValidatorFact return this.beanFactory.createBean(key); } + // Bean Validation 1.1 releaseInstance method public void releaseInstance(ConstraintValidator instance) { this.beanFactory.destroyBean(instance); } diff --git a/spring-web/src/main/java/org/springframework/web/bind/support/SpringWebConstraintValidatorFactory.java b/spring-web/src/main/java/org/springframework/web/bind/support/SpringWebConstraintValidatorFactory.java new file mode 100644 index 0000000000..09b6966355 --- /dev/null +++ b/spring-web/src/main/java/org/springframework/web/bind/support/SpringWebConstraintValidatorFactory.java @@ -0,0 +1,69 @@ +/* + * Copyright 2002-2015 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.web.bind.support; + +import javax.validation.ConstraintValidator; +import javax.validation.ConstraintValidatorFactory; + +import org.springframework.web.context.ContextLoader; +import org.springframework.web.context.WebApplicationContext; + +/** + * JSR-303 {@link ConstraintValidatorFactory} implementation that delegates to + * the current Spring {@link WebApplicationContext} for creating autowired + * {@link ConstraintValidator} instances. + * + *

In contrast to + * {@link org.springframework.validation.beanvalidation.SpringConstraintValidatorFactory}, + * this variant is meant for declarative use in a standard {@code validation.xml} file, + * e.g. in combination with JAX-RS or JAX-WS. + * + * @author Juergen Hoeller + * @since 4.2.1 + * @see ContextLoader#getCurrentWebApplicationContext() + * @see org.springframework.validation.beanvalidation.SpringConstraintValidatorFactory + */ +public class SpringWebConstraintValidatorFactory implements ConstraintValidatorFactory { + + @Override + public > T getInstance(Class key) { + return getWebApplicationContext().getAutowireCapableBeanFactory().createBean(key); + } + + // Bean Validation 1.1 releaseInstance method + public void releaseInstance(ConstraintValidator instance) { + getWebApplicationContext().getAutowireCapableBeanFactory().destroyBean(instance); + } + + + /** + * Retrieve the Spring {@link WebApplicationContext} to use. + * The default implementation returns the current {@link WebApplicationContext} + * as registered for the thread context class loader. + * @return the current WebApplicationContext (never {@code null}) + * @see ContextLoader#getCurrentWebApplicationContext() + */ + protected WebApplicationContext getWebApplicationContext() { + WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext(); + if (wac == null) { + throw new IllegalStateException("No WebApplicationContext registered for current thread - " + + "consider overriding SpringWebConstraintValidatorFactory.getWebApplicationContext()"); + } + return wac; + } + +}