BeanValidationPostProcessor validates singleton target behind proxy
Issue: SPR-17273
This commit is contained in:
parent
cbc0fad961
commit
77887ef739
|
@ -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();) {
|
||||
|
|
|
@ -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.
|
||||
|
@ -22,10 +22,13 @@ import javax.validation.constraints.Size;
|
|||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
|
||||
import org.springframework.beans.factory.BeanCreationException;
|
||||
import org.springframework.beans.factory.support.RootBeanDefinition;
|
||||
import org.springframework.context.annotation.CommonAnnotationBeanPostProcessor;
|
||||
import org.springframework.context.support.GenericApplicationContext;
|
||||
import org.springframework.scheduling.annotation.Async;
|
||||
import org.springframework.scheduling.annotation.AsyncAnnotationAdvisor;
|
||||
import org.springframework.tests.sample.beans.TestBean;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
@ -76,6 +79,20 @@ public class BeanValidationPostProcessorTests {
|
|||
ac.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNotNullConstraintAfterInitializationWithProxy() {
|
||||
GenericApplicationContext ac = new GenericApplicationContext();
|
||||
RootBeanDefinition bvpp = new RootBeanDefinition(BeanValidationPostProcessor.class);
|
||||
bvpp.getPropertyValues().add("afterInitialization", true);
|
||||
ac.registerBeanDefinition("bvpp", bvpp);
|
||||
ac.registerBeanDefinition("capp", new RootBeanDefinition(CommonAnnotationBeanPostProcessor.class));
|
||||
ac.registerBeanDefinition("bean", new RootBeanDefinition(AfterInitConstraintBean.class));
|
||||
ac.registerBeanDefinition("autoProxyCreator", new RootBeanDefinition(DefaultAdvisorAutoProxyCreator.class));
|
||||
ac.registerBeanDefinition("asyncAdvisor", new RootBeanDefinition(AsyncAnnotationAdvisor.class));
|
||||
ac.refresh();
|
||||
ac.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSizeConstraint() {
|
||||
GenericApplicationContext ac = new GenericApplicationContext();
|
||||
|
@ -156,6 +173,10 @@ public class BeanValidationPostProcessorTests {
|
|||
public void init() {
|
||||
this.testBean = new TestBean();
|
||||
}
|
||||
|
||||
@Async
|
||||
void asyncMethod() {
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue