BeanValidationPostProcessor validates singleton target behind proxy
Issue: SPR-17273
This commit is contained in:
@@ -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();) {
|
||||
|
||||
Reference in New Issue
Block a user