From 42eec50e90e6e5264bb3a11e8270af5cb17a4721 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Thu, 27 Jul 2017 08:38:25 +0100 Subject: [PATCH] Perform background preinitialization once per class loader Background preinitialization triggers static initialization of a number of components that are slow to initialize. As the initialization is static, it's only necessary once per class loader. Previously, a new background preinitialization thread would be created and started for each ApplicationEnvironmentPreparedEvent. This commit updates the preinitializer to only create and start the thread if preinitialization has not already been started for the current class loader. Closes gh-9869 --- .../boot/autoconfigure/BackgroundPreinitializer.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/BackgroundPreinitializer.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/BackgroundPreinitializer.java index 43de3e2285f..73860b422cf 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/BackgroundPreinitializer.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/BackgroundPreinitializer.java @@ -16,6 +16,8 @@ package org.springframework.boot.autoconfigure; +import java.util.concurrent.atomic.AtomicBoolean; + import javax.validation.Validation; import org.apache.catalina.mbeans.MBeanFactory; @@ -40,8 +42,16 @@ import org.springframework.http.converter.support.AllEncompassingFormHttpMessage public class BackgroundPreinitializer implements ApplicationListener { + private static final AtomicBoolean preinitalizationStarted = new AtomicBoolean(false); + @Override public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) { + if (preinitalizationStarted.compareAndSet(false, true)) { + performPreinitialization(); + } + } + + private void performPreinitialization() { try { Thread thread = new Thread(new Runnable() {