Skip to content

Commit 77887ef

Browse files
committed
BeanValidationPostProcessor validates singleton target behind proxy
Issue: SPR-17273
1 parent cbc0fad commit 77887ef

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

spring-context/src/main/java/org/springframework/validation/beanvalidation/BeanValidationPostProcessor.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,6 +23,7 @@
2323
import javax.validation.Validator;
2424
import javax.validation.ValidatorFactory;
2525

26+
import org.springframework.aop.framework.AopProxyUtils;
2627
import org.springframework.beans.BeansException;
2728
import org.springframework.beans.factory.BeanInitializationException;
2829
import org.springframework.beans.factory.InitializingBean;
@@ -107,7 +108,12 @@ public Object postProcessAfterInitialization(Object bean, String beanName) throw
107108
*/
108109
protected void doValidate(Object bean) {
109110
Assert.state(this.validator != null, "No Validator set");
110-
Set<ConstraintViolation<Object>> result = this.validator.validate(bean);
111+
Object objectToValidate = AopProxyUtils.getSingletonTarget(bean);
112+
if (objectToValidate == null) {
113+
objectToValidate = bean;
114+
}
115+
Set<ConstraintViolation<Object>> result = this.validator.validate(objectToValidate);
116+
111117
if (!result.isEmpty()) {
112118
StringBuilder sb = new StringBuilder("Bean state is invalid: ");
113119
for (Iterator<ConstraintViolation<Object>> it = result.iterator(); it.hasNext();) {

spring-context/src/test/java/org/springframework/validation/beanvalidation/BeanValidationPostProcessorTests.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -22,10 +22,13 @@
2222

2323
import org.junit.Test;
2424

25+
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
2526
import org.springframework.beans.factory.BeanCreationException;
2627
import org.springframework.beans.factory.support.RootBeanDefinition;
2728
import org.springframework.context.annotation.CommonAnnotationBeanPostProcessor;
2829
import org.springframework.context.support.GenericApplicationContext;
30+
import org.springframework.scheduling.annotation.Async;
31+
import org.springframework.scheduling.annotation.AsyncAnnotationAdvisor;
2932
import org.springframework.tests.sample.beans.TestBean;
3033

3134
import static org.junit.Assert.*;
@@ -76,6 +79,20 @@ public void testNotNullConstraintAfterInitialization() {
7679
ac.close();
7780
}
7881

82+
@Test
83+
public void testNotNullConstraintAfterInitializationWithProxy() {
84+
GenericApplicationContext ac = new GenericApplicationContext();
85+
RootBeanDefinition bvpp = new RootBeanDefinition(BeanValidationPostProcessor.class);
86+
bvpp.getPropertyValues().add("afterInitialization", true);
87+
ac.registerBeanDefinition("bvpp", bvpp);
88+
ac.registerBeanDefinition("capp", new RootBeanDefinition(CommonAnnotationBeanPostProcessor.class));
89+
ac.registerBeanDefinition("bean", new RootBeanDefinition(AfterInitConstraintBean.class));
90+
ac.registerBeanDefinition("autoProxyCreator", new RootBeanDefinition(DefaultAdvisorAutoProxyCreator.class));
91+
ac.registerBeanDefinition("asyncAdvisor", new RootBeanDefinition(AsyncAnnotationAdvisor.class));
92+
ac.refresh();
93+
ac.close();
94+
}
95+
7996
@Test
8097
public void testSizeConstraint() {
8198
GenericApplicationContext ac = new GenericApplicationContext();
@@ -156,6 +173,10 @@ public void setTestBean(TestBean testBean) {
156173
public void init() {
157174
this.testBean = new TestBean();
158175
}
176+
177+
@Async
178+
void asyncMethod() {
179+
}
159180
}
160181

161182
}

0 commit comments

Comments
 (0)