diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/BackgroundPreinitializer.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/BackgroundPreinitializer.java index 20a74abd204..a98f80b6928 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/BackgroundPreinitializer.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/BackgroundPreinitializer.java @@ -135,7 +135,7 @@ public class BackgroundPreinitializer @Override public void run() { - Validation.byDefaultProvider().configure(); + Validation.byDefaultProvider().configure().buildValidatorFactory(); } } diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/Jsr303ConfigurationPropertiesValidator.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/Jsr303ConfigurationPropertiesValidator.java index 2582ec8d2e1..598efb8fca9 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/Jsr303ConfigurationPropertiesValidator.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/Jsr303ConfigurationPropertiesValidator.java @@ -16,18 +16,9 @@ package org.springframework.boot.context.properties; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; -import java.util.concurrent.Future; - import org.springframework.boot.validation.MessageInterpolatorFactory; import org.springframework.context.ApplicationContext; -import org.springframework.context.ApplicationListener; -import org.springframework.context.ConfigurableApplicationContext; -import org.springframework.context.event.ContextRefreshedEvent; import org.springframework.core.annotation.AnnotatedElementUtils; -import org.springframework.util.ReflectionUtils; import org.springframework.validation.Errors; import org.springframework.validation.Validator; import org.springframework.validation.annotation.Validated; @@ -41,41 +32,21 @@ import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean; */ class Jsr303ConfigurationPropertiesValidator implements Validator { - private final Future delegate; + private final Delegate delegate; Jsr303ConfigurationPropertiesValidator(ApplicationContext applicationContext) { - // Creating the delegate is slow so we do it in the background - ExecutorService executor = Executors.newSingleThreadExecutor(); - this.delegate = executor.submit(() -> new Delegate(applicationContext)); - executor.shutdown(); - if (applicationContext instanceof ConfigurableApplicationContext) { - ((ConfigurableApplicationContext) applicationContext) - .addApplicationListener(new Listener()); - } + this.delegate = new Delegate(applicationContext); } @Override public boolean supports(Class type) { return AnnotatedElementUtils.hasAnnotation(type, Validated.class) - && getDelegate().supports(type); + && this.delegate.supports(type); } @Override public void validate(Object target, Errors errors) { - getDelegate().validate(target, errors); - } - - private Delegate getDelegate() { - try { - return this.delegate.get(); - } - catch (InterruptedException ex) { - throw new IllegalStateException(ex); - } - catch (ExecutionException ex) { - ReflectionUtils.rethrowRuntimeException(ex.getCause()); - return null; - } + this.delegate.validate(target, errors); } private static class Delegate extends LocalValidatorFactoryBean { @@ -88,13 +59,4 @@ class Jsr303ConfigurationPropertiesValidator implements Validator { } - private class Listener implements ApplicationListener { - - @Override - public void onApplicationEvent(ContextRefreshedEvent event) { - getDelegate(); - } - - } - }