001/* 002 * Copyright 2002-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 * https://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.validation.beanvalidation; 018 019import javax.validation.MessageInterpolator; 020import javax.validation.TraversableResolver; 021import javax.validation.Validation; 022import javax.validation.Validator; 023import javax.validation.ValidatorContext; 024import javax.validation.ValidatorFactory; 025 026import org.springframework.beans.factory.InitializingBean; 027import org.springframework.lang.Nullable; 028 029/** 030 * Configurable bean class that exposes a specific JSR-303 Validator 031 * through its original interface as well as through the Spring 032 * {@link org.springframework.validation.Validator} interface. 033 * 034 * @author Juergen Hoeller 035 * @since 3.0 036 */ 037public class CustomValidatorBean extends SpringValidatorAdapter implements Validator, InitializingBean { 038 039 @Nullable 040 private ValidatorFactory validatorFactory; 041 042 @Nullable 043 private MessageInterpolator messageInterpolator; 044 045 @Nullable 046 private TraversableResolver traversableResolver; 047 048 049 /** 050 * Set the ValidatorFactory to obtain the target Validator from. 051 * <p>Default is {@link javax.validation.Validation#buildDefaultValidatorFactory()}. 052 */ 053 public void setValidatorFactory(ValidatorFactory validatorFactory) { 054 this.validatorFactory = validatorFactory; 055 } 056 057 /** 058 * Specify a custom MessageInterpolator to use for this Validator. 059 */ 060 public void setMessageInterpolator(MessageInterpolator messageInterpolator) { 061 this.messageInterpolator = messageInterpolator; 062 } 063 064 /** 065 * Specify a custom TraversableResolver to use for this Validator. 066 */ 067 public void setTraversableResolver(TraversableResolver traversableResolver) { 068 this.traversableResolver = traversableResolver; 069 } 070 071 072 @Override 073 public void afterPropertiesSet() { 074 if (this.validatorFactory == null) { 075 this.validatorFactory = Validation.buildDefaultValidatorFactory(); 076 } 077 078 ValidatorContext validatorContext = this.validatorFactory.usingContext(); 079 MessageInterpolator targetInterpolator = this.messageInterpolator; 080 if (targetInterpolator == null) { 081 targetInterpolator = this.validatorFactory.getMessageInterpolator(); 082 } 083 validatorContext.messageInterpolator(new LocaleContextMessageInterpolator(targetInterpolator)); 084 if (this.traversableResolver != null) { 085 validatorContext.traversableResolver(this.traversableResolver); 086 } 087 088 setTargetValidator(validatorContext.getValidator()); 089 } 090 091}