Commit 37fe7bc8 authored by Phillip Webb's avatar Phillip Webb

Update validator background initializer

Update the validator background initializer to actually create
the validator.

Closes gh-11412
parent 732d9868
...@@ -135,7 +135,7 @@ public class BackgroundPreinitializer ...@@ -135,7 +135,7 @@ public class BackgroundPreinitializer
@Override @Override
public void run() { public void run() {
Validation.byDefaultProvider().configure(); Validation.byDefaultProvider().configure().buildValidatorFactory();
} }
} }
......
...@@ -16,18 +16,9 @@ ...@@ -16,18 +16,9 @@
package org.springframework.boot.context.properties; 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.boot.validation.MessageInterpolatorFactory;
import org.springframework.context.ApplicationContext; 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.core.annotation.AnnotatedElementUtils;
import org.springframework.util.ReflectionUtils;
import org.springframework.validation.Errors; import org.springframework.validation.Errors;
import org.springframework.validation.Validator; import org.springframework.validation.Validator;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
...@@ -41,41 +32,21 @@ import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean; ...@@ -41,41 +32,21 @@ import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
*/ */
class Jsr303ConfigurationPropertiesValidator implements Validator { class Jsr303ConfigurationPropertiesValidator implements Validator {
private final Future<Delegate> delegate; private final Delegate delegate;
Jsr303ConfigurationPropertiesValidator(ApplicationContext applicationContext) { Jsr303ConfigurationPropertiesValidator(ApplicationContext applicationContext) {
// Creating the delegate is slow so we do it in the background this.delegate = new Delegate(applicationContext);
ExecutorService executor = Executors.newSingleThreadExecutor();
this.delegate = executor.submit(() -> new Delegate(applicationContext));
executor.shutdown();
if (applicationContext instanceof ConfigurableApplicationContext) {
((ConfigurableApplicationContext) applicationContext)
.addApplicationListener(new Listener());
}
} }
@Override @Override
public boolean supports(Class<?> type) { public boolean supports(Class<?> type) {
return AnnotatedElementUtils.hasAnnotation(type, Validated.class) return AnnotatedElementUtils.hasAnnotation(type, Validated.class)
&& getDelegate().supports(type); && this.delegate.supports(type);
} }
@Override @Override
public void validate(Object target, Errors errors) { public void validate(Object target, Errors errors) {
getDelegate().validate(target, errors); this.delegate.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;
}
} }
private static class Delegate extends LocalValidatorFactoryBean { private static class Delegate extends LocalValidatorFactoryBean {
...@@ -88,13 +59,4 @@ class Jsr303ConfigurationPropertiesValidator implements Validator { ...@@ -88,13 +59,4 @@ class Jsr303ConfigurationPropertiesValidator implements Validator {
} }
private class Listener implements ApplicationListener<ContextRefreshedEvent> {
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
getDelegate();
}
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment