Polish 'Refine BackgroundPreinitializer Jackson initialization'

See gh-33220
This commit is contained in:
Phillip Webb 2022-11-16 12:01:05 -08:00
parent bdac416a62
commit 6cc6a15edf
1 changed files with 6 additions and 9 deletions

View File

@ -34,7 +34,6 @@ import org.springframework.core.annotation.Order;
import org.springframework.format.support.DefaultFormattingConversionService; import org.springframework.format.support.DefaultFormattingConversionService;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder; import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter; import org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter;
import org.springframework.util.ClassUtils;
/** /**
* {@link ApplicationListener} to trigger early initialization in a background thread of * {@link ApplicationListener} to trigger early initialization in a background thread of
@ -101,24 +100,22 @@ public class BackgroundPreinitializer implements ApplicationListener<SpringAppli
public void run() { public void run() {
runSafely(new ConversionServiceInitializer()); runSafely(new ConversionServiceInitializer());
runSafely(new ValidationInitializer()); runSafely(new ValidationInitializer());
if (ClassUtils.isPresent( if (!runSafely(new MessageConverterInitializer())) {
"org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter", // If the MessageConverterInitializer we still might be able to
BackgroundPreinitializer.class.getClassLoader())) { // initialize Jackson
runSafely(new MessageConverterInitializer());
}
else {
runSafely(new JacksonInitializer()); runSafely(new JacksonInitializer());
} }
runSafely(new CharsetInitializer()); runSafely(new CharsetInitializer());
preinitializationComplete.countDown(); preinitializationComplete.countDown();
} }
public void runSafely(Runnable runnable) { boolean runSafely(Runnable runnable) {
try { try {
runnable.run(); runnable.run();
return true;
} }
catch (Throwable ex) { catch (Throwable ex) {
// Ignore return false;
} }
} }