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
This commit is contained in:
Andy Wilkinson 2017-07-27 08:38:25 +01:00
parent 6f864c6210
commit 42eec50e90
1 changed files with 10 additions and 0 deletions

View File

@ -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<ApplicationEnvironmentPreparedEvent> {
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() {