BeanValidationPostProcessor validates singleton target behind proxy

Issue: SPR-17273
This commit is contained in:
Juergen Hoeller
2018-09-13 20:24:27 +02:00
parent cbc0fad961
commit 77887ef739
2 changed files with 30 additions and 3 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 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.
@@ -23,6 +23,7 @@ import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
import org.springframework.aop.framework.AopProxyUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanInitializationException;
import org.springframework.beans.factory.InitializingBean;
@@ -107,7 +108,12 @@ public class BeanValidationPostProcessor implements BeanPostProcessor, Initializ
*/
protected void doValidate(Object bean) {
Assert.state(this.validator != null, "No Validator set");
Set<ConstraintViolation<Object>> result = this.validator.validate(bean);
Object objectToValidate = AopProxyUtils.getSingletonTarget(bean);
if (objectToValidate == null) {
objectToValidate = bean;
}
Set<ConstraintViolation<Object>> result = this.validator.validate(objectToValidate);
if (!result.isEmpty()) {
StringBuilder sb = new StringBuilder("Bean state is invalid: ");
for (Iterator<ConstraintViolation<Object>> it = result.iterator(); it.hasNext();) {