|
32 | 32 | import org.springframework.aop.framework.ProxyFactory;
|
33 | 33 | import org.springframework.beans.MutablePropertyValues;
|
34 | 34 | import org.springframework.beans.factory.FactoryBean;
|
| 35 | +import org.springframework.beans.factory.ObjectProvider; |
35 | 36 | import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
36 | 37 | import org.springframework.context.annotation.Bean;
|
37 | 38 | import org.springframework.context.annotation.Configuration;
|
|
54 | 55 | * and/or {@link MethodValidationPostProcessor}.
|
55 | 56 | *
|
56 | 57 | * @author Juergen Hoeller
|
57 |
| - * @author Rossen Stoyanchevß |
| 58 | + * @author Rossen Stoyanchev |
58 | 59 | */
|
59 | 60 | public class MethodValidationProxyTests {
|
60 | 61 |
|
@@ -109,19 +110,29 @@ private void doTestProxyValidation(MyValidInterface<String> proxy) {
|
109 | 110 |
|
110 | 111 | @Test
|
111 | 112 | public void testLazyValidatorForMethodValidation() {
|
112 |
| - AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext( |
113 |
| - LazyMethodValidationConfig.class, CustomValidatorBean.class, |
114 |
| - MyValidBean.class, MyValidFactoryBean.class); |
115 |
| - context.getBeansOfType(MyValidInterface.class).values().forEach(bean -> bean.myValidMethod("value", 5)); |
116 |
| - context.close(); |
| 113 | + doTestLazyValidatorForMethodValidation(LazyMethodValidationConfig.class); |
117 | 114 | }
|
118 | 115 |
|
119 | 116 | @Test
|
120 | 117 | public void testLazyValidatorForMethodValidationWithProxyTargetClass() {
|
121 |
| - AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext( |
122 |
| - LazyMethodValidationConfigWithProxyTargetClass.class, CustomValidatorBean.class, |
123 |
| - MyValidBean.class, MyValidFactoryBean.class); |
| 118 | + doTestLazyValidatorForMethodValidation(LazyMethodValidationConfigWithProxyTargetClass.class); |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + public void testLazyValidatorForMethodValidationWithValidatorProvider() { |
| 123 | + doTestLazyValidatorForMethodValidation(LazyMethodValidationConfigWithValidatorProvider.class); |
| 124 | + } |
| 125 | + |
| 126 | + private void doTestLazyValidatorForMethodValidation(Class<?> configClass) { |
| 127 | + AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); |
| 128 | + context.register(configClass, CustomValidatorBean.class, MyValidBean.class, MyValidFactoryBean.class); |
| 129 | + context.getDefaultListableBeanFactory().getBeanDefinition("customValidatorBean").setLazyInit(true); |
| 130 | + context.refresh(); |
| 131 | + |
| 132 | + assertThat(context.getDefaultListableBeanFactory().containsSingleton("customValidatorBean")).isFalse(); |
124 | 133 | context.getBeansOfType(MyValidInterface.class).values().forEach(bean -> bean.myValidMethod("value", 5));
|
| 134 | + assertThat(context.getDefaultListableBeanFactory().containsSingleton("customValidatorBean")).isTrue(); |
| 135 | + |
125 | 136 | context.close();
|
126 | 137 | }
|
127 | 138 |
|
@@ -256,4 +267,16 @@ public static MethodValidationPostProcessor methodValidationPostProcessor(@Lazy
|
256 | 267 | }
|
257 | 268 | }
|
258 | 269 |
|
| 270 | + |
| 271 | + @Configuration |
| 272 | + public static class LazyMethodValidationConfigWithValidatorProvider { |
| 273 | + |
| 274 | + @Bean |
| 275 | + public static MethodValidationPostProcessor methodValidationPostProcessor(ObjectProvider<Validator> validator) { |
| 276 | + MethodValidationPostProcessor postProcessor = new MethodValidationPostProcessor(); |
| 277 | + postProcessor.setValidatorProvider(validator); |
| 278 | + return postProcessor; |
| 279 | + } |
| 280 | + } |
| 281 | + |
259 | 282 | }
|
0 commit comments