|
16 | 16 |
|
17 | 17 | package org.springframework.boot.autoconfigure.validation; |
18 | 18 |
|
| 19 | +import java.util.HashSet; |
| 20 | +import java.util.Set; |
| 21 | + |
19 | 22 | import javax.validation.ConstraintViolationException; |
20 | 23 | import javax.validation.Validator; |
21 | 24 | import javax.validation.constraints.Min; |
|
27 | 30 | import org.junit.rules.ExpectedException; |
28 | 31 |
|
29 | 32 | import org.springframework.beans.DirectFieldAccessor; |
| 33 | +import org.springframework.beans.factory.config.BeanPostProcessor; |
| 34 | +import org.springframework.boot.autoconfigure.validation.ValidationAutoConfigurationTests.CustomValidatorConfiguration.TestBeanPostProcessor; |
30 | 35 | import org.springframework.boot.test.util.TestPropertyValues; |
31 | 36 | import org.springframework.context.annotation.AnnotationConfigApplicationContext; |
32 | 37 | import org.springframework.context.annotation.Bean; |
33 | 38 | import org.springframework.context.annotation.Configuration; |
34 | 39 | import org.springframework.context.annotation.Primary; |
35 | 40 | import org.springframework.validation.annotation.Validated; |
| 41 | +import org.springframework.validation.beanvalidation.CustomValidatorBean; |
36 | 42 | import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean; |
37 | 43 | import org.springframework.validation.beanvalidation.MethodValidationPostProcessor; |
38 | 44 | import org.springframework.validation.beanvalidation.OptionalValidatorFactoryBean; |
@@ -198,6 +204,13 @@ public void userDefinedMethodValidationPostProcessorTakesPrecedence() { |
198 | 204 | .getPropertyValue("validator")); |
199 | 205 | } |
200 | 206 |
|
| 207 | + @Test |
| 208 | + public void methodValidationPostProcessorValidatorDependencyDoesNotTriggerEarlyInitialization() { |
| 209 | + load(CustomValidatorConfiguration.class); |
| 210 | + assertThat(this.context.getBean(TestBeanPostProcessor.class).postProcessed) |
| 211 | + .contains("someService"); |
| 212 | + } |
| 213 | + |
201 | 214 | private boolean isPrimaryBean(String beanName) { |
202 | 215 | return this.context.getBeanDefinition(beanName).isPrimary(); |
203 | 216 | } |
@@ -322,4 +335,53 @@ public MethodValidationPostProcessor testMethodValidationPostProcessor() { |
322 | 335 |
|
323 | 336 | } |
324 | 337 |
|
| 338 | + @Configuration |
| 339 | + static class CustomValidatorConfiguration { |
| 340 | + |
| 341 | + CustomValidatorConfiguration(SomeService someService) { |
| 342 | + |
| 343 | + } |
| 344 | + |
| 345 | + @Bean |
| 346 | + Validator customValidator() { |
| 347 | + return new CustomValidatorBean(); |
| 348 | + } |
| 349 | + |
| 350 | + @Bean |
| 351 | + static TestBeanPostProcessor testBeanPostProcessor() { |
| 352 | + return new TestBeanPostProcessor(); |
| 353 | + } |
| 354 | + |
| 355 | + @Configuration |
| 356 | + static class SomeServiceConfiguration { |
| 357 | + |
| 358 | + @Bean |
| 359 | + public SomeService someService() { |
| 360 | + return new SomeService(); |
| 361 | + } |
| 362 | + |
| 363 | + } |
| 364 | + |
| 365 | + static class SomeService { |
| 366 | + |
| 367 | + } |
| 368 | + |
| 369 | + static class TestBeanPostProcessor implements BeanPostProcessor { |
| 370 | + |
| 371 | + private Set<String> postProcessed = new HashSet<String>(); |
| 372 | + |
| 373 | + @Override |
| 374 | + public Object postProcessAfterInitialization(Object bean, String name) { |
| 375 | + this.postProcessed.add(name); |
| 376 | + return bean; |
| 377 | + } |
| 378 | + |
| 379 | + @Override |
| 380 | + public Object postProcessBeforeInitialization(Object bean, String name) { |
| 381 | + return bean; |
| 382 | + } |
| 383 | + |
| 384 | + } |
| 385 | + } |
| 386 | + |
325 | 387 | } |
0 commit comments