Update validator background initializer

Update the validator background initializer to actually create
the validator.

Closes gh-11412
This commit is contained in:
Phillip Webb 2017-12-29 23:46:50 -08:00
parent 732d9868e3
commit 37fe7bc8f6
2 changed files with 5 additions and 43 deletions

View File

@ -135,7 +135,7 @@ public class BackgroundPreinitializer
@Override
public void run() {
Validation.byDefaultProvider().configure();
Validation.byDefaultProvider().configure().buildValidatorFactory();
}
}

View File

@ -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> 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<ContextRefreshedEvent> {
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
getDelegate();
}
}
}