SpringWebConstraintValidatorFactory for use with validation.xml

Issue: SPR-13327
This commit is contained in:
Juergen Hoeller
2015-08-21 17:02:55 +02:00
parent 79e24aeced
commit 6d1b8b5a31
3 changed files with 77 additions and 1 deletions

View File

@@ -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")

View File

@@ -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.
*
* <p>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);
}

View File

@@ -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.
*
* <p>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 extends ConstraintValidator<?, ?>> T getInstance(Class<T> 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;
}
}