|
| 1 | +/* |
| 2 | + * Copyright 2002-2011 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.validation.beanvalidation; |
| 18 | + |
| 19 | +import java.lang.annotation.Annotation; |
| 20 | +import javax.validation.Validator; |
| 21 | +import javax.validation.ValidatorFactory; |
| 22 | + |
| 23 | +import org.aopalliance.aop.Advice; |
| 24 | + |
| 25 | +import org.springframework.aop.Advisor; |
| 26 | +import org.springframework.aop.Pointcut; |
| 27 | +import org.springframework.aop.framework.Advised; |
| 28 | +import org.springframework.aop.framework.AopInfrastructureBean; |
| 29 | +import org.springframework.aop.framework.ProxyConfig; |
| 30 | +import org.springframework.aop.framework.ProxyFactory; |
| 31 | +import org.springframework.aop.support.AopUtils; |
| 32 | +import org.springframework.aop.support.DefaultPointcutAdvisor; |
| 33 | +import org.springframework.aop.support.annotation.AnnotationMatchingPointcut; |
| 34 | +import org.springframework.beans.BeansException; |
| 35 | +import org.springframework.beans.factory.BeanClassLoaderAware; |
| 36 | +import org.springframework.beans.factory.InitializingBean; |
| 37 | +import org.springframework.beans.factory.config.BeanPostProcessor; |
| 38 | +import org.springframework.core.Ordered; |
| 39 | +import org.springframework.util.Assert; |
| 40 | +import org.springframework.util.ClassUtils; |
| 41 | +import org.springframework.validation.annotation.Validated; |
| 42 | + |
| 43 | +/** |
| 44 | + * A convenient {@link BeanPostProcessor} implementation that delegates to a |
| 45 | + * JSR-303 provider for performing method-level validation on annotated methods. |
| 46 | + * |
| 47 | + * <p>Applicable methods have JSR-303 constraint annotations on their parameters |
| 48 | + * and/or on their return value (in the latter case specified at the method level, |
| 49 | + * typically as inline annotation). |
| 50 | + * |
| 51 | + * <p>E.g.: <code>public @NotNull Object myValidMethod(@NotNull String arg1, @Max(10) int arg2)</code> |
| 52 | + * |
| 53 | + * <p>Target classes with such annotated methods need to be annotated with Spring's |
| 54 | + * {@link Validated} annotation at the type level, for their methods to be searched for |
| 55 | + * inline constraint annotations. Validation groups can be specified through {@link Validated} |
| 56 | + * as well. By default, JSR-303 will validate against its default group only. |
| 57 | + * |
| 58 | + * <p>As of Spring 3.1, this functionality requires Hibernate Validator 4.2 or higher. |
| 59 | + * In Spring 3.2, this class will autodetect a Bean Validation 1.1 compliant provider |
| 60 | + * and automatically use the standard method validation support there (once available). |
| 61 | + * |
| 62 | + * @author Juergen Hoeller |
| 63 | + * @since 3.1 |
| 64 | + * @see MethodValidationInterceptor |
| 65 | + * @see org.hibernate.validator.method.MethodValidator |
| 66 | + */ |
| 67 | +public class MethodValidationPostProcessor extends ProxyConfig |
| 68 | + implements BeanPostProcessor, BeanClassLoaderAware, Ordered, InitializingBean { |
| 69 | + |
| 70 | + private Class<? extends Annotation> validatedAnnotationType = Validated.class; |
| 71 | + |
| 72 | + private Validator validator; |
| 73 | + |
| 74 | + private ClassLoader beanClassLoader = ClassUtils.getDefaultClassLoader(); |
| 75 | + |
| 76 | + private Advisor advisor; |
| 77 | + |
| 78 | + |
| 79 | + /** |
| 80 | + * Set the 'validated' annotation type. |
| 81 | + * The default validated annotation type is the {@link Validated} annotation. |
| 82 | + * <p>This setter property exists so that developers can provide their own |
| 83 | + * (non-Spring-specific) annotation type to indicate that a class is supposed |
| 84 | + * to be validated in the sense of applying method validation. |
| 85 | + * @param validatedAnnotationType the desired annotation type |
| 86 | + */ |
| 87 | + public void setValidatedAnnotationType(Class<? extends Annotation> validatedAnnotationType) { |
| 88 | + Assert.notNull(validatedAnnotationType, "'validatedAnnotationType' must not be null"); |
| 89 | + this.validatedAnnotationType = validatedAnnotationType; |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Set the JSR-303 Validator to delegate to for validating methods. |
| 94 | + * <p>Default is the default ValidatorFactory's default Validator. |
| 95 | + */ |
| 96 | + public void setValidator(Validator validator) { |
| 97 | + this.validator = validator; |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Set the JSR-303 ValidatorFactory to delegate to for validating methods, |
| 102 | + * using its default Validator. |
| 103 | + * <p>Default is the default ValidatorFactory's default Validator. |
| 104 | + * @see javax.validation.ValidatorFactory#getValidator() |
| 105 | + */ |
| 106 | + public void setValidatorFactory(ValidatorFactory validatorFactory) { |
| 107 | + this.validator = validatorFactory.getValidator(); |
| 108 | + } |
| 109 | + |
| 110 | + public void setBeanClassLoader(ClassLoader classLoader) { |
| 111 | + this.beanClassLoader = classLoader; |
| 112 | + } |
| 113 | + |
| 114 | + public int getOrder() { |
| 115 | + // This should run after all other post-processors, so that it can just add |
| 116 | + // an advisor to existing proxies rather than double-proxy. |
| 117 | + return LOWEST_PRECEDENCE; |
| 118 | + } |
| 119 | + |
| 120 | + |
| 121 | + public void afterPropertiesSet() { |
| 122 | + Pointcut pointcut = new AnnotationMatchingPointcut(this.validatedAnnotationType, true); |
| 123 | + Advice advice = (this.validator != null ? new MethodValidationInterceptor(this.validator) : |
| 124 | + new MethodValidationInterceptor()); |
| 125 | + this.advisor = new DefaultPointcutAdvisor(pointcut, advice); |
| 126 | + } |
| 127 | + |
| 128 | + |
| 129 | + public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { |
| 130 | + return bean; |
| 131 | + } |
| 132 | + |
| 133 | + public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { |
| 134 | + if (bean instanceof AopInfrastructureBean) { |
| 135 | + // Ignore AOP infrastructure such as scoped proxies. |
| 136 | + return bean; |
| 137 | + } |
| 138 | + Class<?> targetClass = AopUtils.getTargetClass(bean); |
| 139 | + if (AopUtils.canApply(this.advisor, targetClass)) { |
| 140 | + if (bean instanceof Advised) { |
| 141 | + ((Advised) bean).addAdvisor(this.advisor); |
| 142 | + return bean; |
| 143 | + } |
| 144 | + else { |
| 145 | + ProxyFactory proxyFactory = new ProxyFactory(bean); |
| 146 | + // Copy our properties (proxyTargetClass etc) inherited from ProxyConfig. |
| 147 | + proxyFactory.copyFrom(this); |
| 148 | + proxyFactory.addAdvisor(this.advisor); |
| 149 | + return proxyFactory.getProxy(this.beanClassLoader); |
| 150 | + } |
| 151 | + } |
| 152 | + else { |
| 153 | + // This is not a repository. |
| 154 | + return bean; |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | +} |
0 commit comments