From 19ce0aa4f07c13bb6dca8b7f9d7173cd77bf8e9d Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Thu, 4 Jan 2018 22:20:16 -0800 Subject: [PATCH] Refine BackgroundPreinitializer Update `BackgroundPreinitializer` to start initialization earlier. Also refine the startup order and initialize Charsets. Fixes gh-11570 See gh-11412 --- .../BackgroundPreinitializer.java | 27 ++++++++++++++----- 1 file changed, 21 insertions(+), 6 deletions(-) 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 a98f80b692..3d4a697224 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 @@ -1,5 +1,5 @@ /* - * Copyright 2012-2017 the original author or authors. + * Copyright 2012-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,16 +16,19 @@ package org.springframework.boot.autoconfigure; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.util.concurrent.CountDownLatch; import java.util.concurrent.atomic.AtomicBoolean; +import javax.validation.Configuration; import javax.validation.Validation; import org.apache.catalina.mbeans.MBeanFactory; -import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent; import org.springframework.boot.context.event.ApplicationFailedEvent; import org.springframework.boot.context.event.ApplicationReadyEvent; +import org.springframework.boot.context.event.ApplicationStartingEvent; import org.springframework.boot.context.event.SpringApplicationEvent; import org.springframework.boot.context.logging.LoggingApplicationListener; import org.springframework.context.ApplicationListener; @@ -53,7 +56,7 @@ public class BackgroundPreinitializer @Override public void onApplicationEvent(SpringApplicationEvent event) { - if (event instanceof ApplicationEnvironmentPreparedEvent) { + if (event instanceof ApplicationStartingEvent) { if (preinitializationStarted.compareAndSet(false, true)) { performPreinitialization(); } @@ -76,11 +79,12 @@ public class BackgroundPreinitializer @Override public void run() { + runSafely(new ConversionServiceInitializer()); + runSafely(new ValidationInitializer()); runSafely(new MessageConverterInitializer()); runSafely(new MBeanFactoryInitializer()); - runSafely(new ValidationInitializer()); runSafely(new JacksonInitializer()); - runSafely(new ConversionServiceInitializer()); + runSafely(new CharsetInitializer()); preinitializationComplete.countDown(); } @@ -135,7 +139,8 @@ public class BackgroundPreinitializer @Override public void run() { - Validation.byDefaultProvider().configure().buildValidatorFactory(); + Configuration configuration = Validation.byDefaultProvider().configure(); + configuration.buildValidatorFactory().getValidator(); } } @@ -164,4 +169,14 @@ public class BackgroundPreinitializer } + private static class CharsetInitializer implements Runnable { + + @Override + public void run() { + StandardCharsets.UTF_8.name(); + Charset.availableCharsets(); + } + + } + }