Skip to content

Commit 19ce0aa

Browse files
committed
Refine BackgroundPreinitializer
Update `BackgroundPreinitializer` to start initialization earlier. Also refine the startup order and initialize Charsets. Fixes gh-11570 See gh-11412
1 parent 79fc883 commit 19ce0aa

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed

Diff for: spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/BackgroundPreinitializer.java

+21-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2017 the original author or authors.
2+
* Copyright 2012-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,16 +16,19 @@
1616

1717
package org.springframework.boot.autoconfigure;
1818

19+
import java.nio.charset.Charset;
20+
import java.nio.charset.StandardCharsets;
1921
import java.util.concurrent.CountDownLatch;
2022
import java.util.concurrent.atomic.AtomicBoolean;
2123

24+
import javax.validation.Configuration;
2225
import javax.validation.Validation;
2326

2427
import org.apache.catalina.mbeans.MBeanFactory;
2528

26-
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
2729
import org.springframework.boot.context.event.ApplicationFailedEvent;
2830
import org.springframework.boot.context.event.ApplicationReadyEvent;
31+
import org.springframework.boot.context.event.ApplicationStartingEvent;
2932
import org.springframework.boot.context.event.SpringApplicationEvent;
3033
import org.springframework.boot.context.logging.LoggingApplicationListener;
3134
import org.springframework.context.ApplicationListener;
@@ -53,7 +56,7 @@ public class BackgroundPreinitializer
5356

5457
@Override
5558
public void onApplicationEvent(SpringApplicationEvent event) {
56-
if (event instanceof ApplicationEnvironmentPreparedEvent) {
59+
if (event instanceof ApplicationStartingEvent) {
5760
if (preinitializationStarted.compareAndSet(false, true)) {
5861
performPreinitialization();
5962
}
@@ -76,11 +79,12 @@ private void performPreinitialization() {
7679

7780
@Override
7881
public void run() {
82+
runSafely(new ConversionServiceInitializer());
83+
runSafely(new ValidationInitializer());
7984
runSafely(new MessageConverterInitializer());
8085
runSafely(new MBeanFactoryInitializer());
81-
runSafely(new ValidationInitializer());
8286
runSafely(new JacksonInitializer());
83-
runSafely(new ConversionServiceInitializer());
87+
runSafely(new CharsetInitializer());
8488
preinitializationComplete.countDown();
8589
}
8690

@@ -135,7 +139,8 @@ private static class ValidationInitializer implements Runnable {
135139

136140
@Override
137141
public void run() {
138-
Validation.byDefaultProvider().configure().buildValidatorFactory();
142+
Configuration<?> configuration = Validation.byDefaultProvider().configure();
143+
configuration.buildValidatorFactory().getValidator();
139144
}
140145

141146
}
@@ -164,4 +169,14 @@ public void run() {
164169

165170
}
166171

172+
private static class CharsetInitializer implements Runnable {
173+
174+
@Override
175+
public void run() {
176+
StandardCharsets.UTF_8.name();
177+
Charset.availableCharsets();
178+
}
179+
180+
}
181+
167182
}

0 commit comments

Comments
 (0)