Skip to content

Commit 42eec50

Browse files
committed
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
1 parent 6f864c6 commit 42eec50

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/BackgroundPreinitializer.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.boot.autoconfigure;
1818

19+
import java.util.concurrent.atomic.AtomicBoolean;
20+
1921
import javax.validation.Validation;
2022

2123
import org.apache.catalina.mbeans.MBeanFactory;
@@ -40,8 +42,16 @@
4042
public class BackgroundPreinitializer
4143
implements ApplicationListener<ApplicationEnvironmentPreparedEvent> {
4244

45+
private static final AtomicBoolean preinitalizationStarted = new AtomicBoolean(false);
46+
4347
@Override
4448
public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
49+
if (preinitalizationStarted.compareAndSet(false, true)) {
50+
performPreinitialization();
51+
}
52+
}
53+
54+
private void performPreinitialization() {
4555
try {
4656
Thread thread = new Thread(new Runnable() {
4757

0 commit comments

Comments
 (0)