001/*
002 * Copyright 2012-2017 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.boot.autoconfigure.validation;
018
019import javax.validation.Validator;
020import javax.validation.executable.ExecutableValidator;
021
022import org.springframework.beans.factory.config.BeanDefinition;
023import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
024import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
025import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
026import org.springframework.boot.autoconfigure.condition.ConditionalOnResource;
027import org.springframework.boot.validation.MessageInterpolatorFactory;
028import org.springframework.context.annotation.Bean;
029import org.springframework.context.annotation.Configuration;
030import org.springframework.context.annotation.Import;
031import org.springframework.context.annotation.Lazy;
032import org.springframework.context.annotation.Role;
033import org.springframework.core.env.Environment;
034import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
035import org.springframework.validation.beanvalidation.MethodValidationPostProcessor;
036
037/**
038 * {@link EnableAutoConfiguration Auto-configuration} to configure the validation
039 * infrastructure.
040 *
041 * @author Stephane Nicoll
042 * @author Madhura Bhave
043 * @since 1.5.0
044 */
045@Configuration
046@ConditionalOnClass(ExecutableValidator.class)
047@ConditionalOnResource(resources = "classpath:META-INF/services/javax.validation.spi.ValidationProvider")
048@Import(PrimaryDefaultValidatorPostProcessor.class)
049public class ValidationAutoConfiguration {
050
051        @Bean
052        @Role(BeanDefinition.ROLE_INFRASTRUCTURE)
053        @ConditionalOnMissingBean(Validator.class)
054        public static LocalValidatorFactoryBean defaultValidator() {
055                LocalValidatorFactoryBean factoryBean = new LocalValidatorFactoryBean();
056                MessageInterpolatorFactory interpolatorFactory = new MessageInterpolatorFactory();
057                factoryBean.setMessageInterpolator(interpolatorFactory.getObject());
058                return factoryBean;
059        }
060
061        @Bean
062        @ConditionalOnMissingBean
063        public static MethodValidationPostProcessor methodValidationPostProcessor(
064                        Environment environment, @Lazy Validator validator) {
065                MethodValidationPostProcessor processor = new MethodValidationPostProcessor();
066                boolean proxyTargetClass = environment
067                                .getProperty("spring.aop.proxy-target-class", Boolean.class, true);
068                processor.setProxyTargetClass(proxyTargetClass);
069                processor.setValidator(validator);
070                return processor;
071        }
072
073}