/*
 * 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
 *
 *      https://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.validation.beanvalidation;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorFactory;

import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.util.Assert;

JSR-303 ConstraintValidatorFactory implementation that delegates to a Spring BeanFactory for creating autowired ConstraintValidator instances.

Note that this class is meant for programmatic use, not for declarative use in a standard validation.xml file. Consider SpringWebConstraintValidatorFactory for declarative use in a web application, e.g. with JAX-RS or JAX-WS.

Author:Juergen Hoeller
See Also:
Since:3.0
/** * 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) * @see org.springframework.context.ApplicationContext#getAutowireCapableBeanFactory() */
public class SpringConstraintValidatorFactory implements ConstraintValidatorFactory { private final AutowireCapableBeanFactory beanFactory;
Create a new SpringConstraintValidatorFactory for the given BeanFactory.
Params:
  • beanFactory – the target BeanFactory
/** * Create a new SpringConstraintValidatorFactory for the given BeanFactory. * @param beanFactory the target BeanFactory */
public SpringConstraintValidatorFactory(AutowireCapableBeanFactory beanFactory) { Assert.notNull(beanFactory, "BeanFactory must not be null"); this.beanFactory = beanFactory; } @Override public <T extends ConstraintValidator<?, ?>> T getInstance(Class<T> key) { return this.beanFactory.createBean(key); } // Bean Validation 1.1 releaseInstance method @Override public void releaseInstance(ConstraintValidator<?, ?> instance) { this.beanFactory.destroyBean(instance); } }