From bdac416a62d2ccb373745171229477c244b00689 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Deleuze?= Date: Wed, 16 Nov 2022 11:47:50 +0100 Subject: [PATCH 1/2] Refine BackgroundPreinitializer Jackson initialization AllEncompassingFormHttpMessageConverter already initializes Jackson ObjectMapper. This commit updates BackgroundPreinitializer in order to not run JacksonInitializer when MessageConverterInitializer already takes care of initializing ObjectMapper. See gh-33220 --- .../boot/autoconfigure/BackgroundPreinitializer.java | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) 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 523bebab482c..6acf7e53d4ad 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 @@ -34,6 +34,7 @@ import org.springframework.format.support.DefaultFormattingConversionService; import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder; import org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter; +import org.springframework.util.ClassUtils; /** * {@link ApplicationListener} to trigger early initialization in a background thread of @@ -100,8 +101,14 @@ private void performPreinitialization() { public void run() { runSafely(new ConversionServiceInitializer()); runSafely(new ValidationInitializer()); - runSafely(new MessageConverterInitializer()); - runSafely(new JacksonInitializer()); + if (ClassUtils.isPresent( + "org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter", + BackgroundPreinitializer.class.getClassLoader())) { + runSafely(new MessageConverterInitializer()); + } + else { + runSafely(new JacksonInitializer()); + } runSafely(new CharsetInitializer()); preinitializationComplete.countDown(); } From 6cc6a15edf18f5409f415e4bdbee6dc77e0b121e Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Wed, 16 Nov 2022 12:01:05 -0800 Subject: [PATCH 2/2] Polish 'Refine BackgroundPreinitializer Jackson initialization' See gh-33220 --- .../autoconfigure/BackgroundPreinitializer.java | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) 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 6acf7e53d4ad..8bdc8c798cc8 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 @@ -34,7 +34,6 @@ import org.springframework.format.support.DefaultFormattingConversionService; import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder; import org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter; -import org.springframework.util.ClassUtils; /** * {@link ApplicationListener} to trigger early initialization in a background thread of @@ -101,24 +100,22 @@ private void performPreinitialization() { public void run() { runSafely(new ConversionServiceInitializer()); runSafely(new ValidationInitializer()); - if (ClassUtils.isPresent( - "org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter", - BackgroundPreinitializer.class.getClassLoader())) { - runSafely(new MessageConverterInitializer()); - } - else { + if (!runSafely(new MessageConverterInitializer())) { + // If the MessageConverterInitializer we still might be able to + // initialize Jackson runSafely(new JacksonInitializer()); } runSafely(new CharsetInitializer()); preinitializationComplete.countDown(); } - public void runSafely(Runnable runnable) { + boolean runSafely(Runnable runnable) { try { runnable.run(); + return true; } catch (Throwable ex) { - // Ignore + return false; } }