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 968dda42f4..6566b819e6 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 @@ -39,15 +39,29 @@ import org.springframework.http.converter.support.AllEncompassingFormHttpMessage /** * {@link ApplicationListener} to trigger early initialization in a background thread of * time consuming tasks. + *

+ * Set the {@value IGNORE_BACKGROUNDPREINITIALIZER_PROPERTY_NAME} system property to + * {@code true} to disable this mechanism and let such initialization happen in the + * foreground. * * @author Phillip Webb * @author Andy Wilkinson + * @author Artsiom Yudovin * @since 1.3.0 */ @Order(LoggingApplicationListener.DEFAULT_ORDER + 1) public class BackgroundPreinitializer implements ApplicationListener { + /** + * System property that instructs Spring Boot how to run pre initialization. When the + * property is set to {@code true}, no pre intialization happens and each item is + * initialized in the foreground as it needs to. When the property is {@code false} + * (default), pre initialization runs in a separate thread in the background. + * @since 2.1.0 + */ + public static final String IGNORE_BACKGROUNDPREINITIALIZER_PROPERTY_NAME = "spring.backgroundpreinitializer.ignore"; + private static final AtomicBoolean preinitializationStarted = new AtomicBoolean( false); @@ -55,7 +69,8 @@ public class BackgroundPreinitializer @Override public void onApplicationEvent(SpringApplicationEvent event) { - if (event instanceof ApplicationStartingEvent + if (!Boolean.getBoolean(IGNORE_BACKGROUNDPREINITIALIZER_PROPERTY_NAME) + && event instanceof ApplicationStartingEvent && preinitializationStarted.compareAndSet(false, true)) { performPreinitialization(); }