From 7b9da786c12f317b687df09900e1bdf27d7e835d Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Tue, 18 Feb 2025 21:35:51 -0800 Subject: [PATCH] Split up BackgroundPreinitializer Issue: 44065 --- .../BackgroundPreinitializer.java | 216 ------------------ .../BackgroundPreinitializer.java | 35 +++ ...undPreinitializingApplicationListener.java | 133 +++++++++++ .../CharsetsBackgroundPreinitializer.java | 33 +++ ...ersionServiceBackgroundPreinitializer.java | 34 +++ .../JacksonBackgroundPreinitializer.java | 33 +++ ...rtaValidationBackgroundPreinitializer.java | 35 +++ ...sageConverterBackgroundPreinitializer.java | 35 +++ .../TomcatBackgroundPreinitializer.java | 35 +++ .../ZoneIdBackgroundPreinitializer.java | 33 +++ .../preinitialize/package-info.java | 20 ++ .../main/resources/META-INF/spring.factories | 12 +- 12 files changed, 437 insertions(+), 217 deletions(-) delete mode 100644 spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/BackgroundPreinitializer.java create mode 100644 spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/preinitialize/BackgroundPreinitializer.java create mode 100644 spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/preinitialize/BackgroundPreinitializingApplicationListener.java create mode 100644 spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/preinitialize/CharsetsBackgroundPreinitializer.java create mode 100644 spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/preinitialize/ConversionServiceBackgroundPreinitializer.java create mode 100644 spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/preinitialize/JacksonBackgroundPreinitializer.java create mode 100644 spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/preinitialize/JakartaValidationBackgroundPreinitializer.java create mode 100644 spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/preinitialize/MessageConverterBackgroundPreinitializer.java create mode 100644 spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/preinitialize/TomcatBackgroundPreinitializer.java create mode 100644 spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/preinitialize/ZoneIdBackgroundPreinitializer.java create mode 100644 spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/preinitialize/package-info.java 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 deleted file mode 100644 index 36ec4abfef6c..000000000000 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/BackgroundPreinitializer.java +++ /dev/null @@ -1,216 +0,0 @@ -/* - * Copyright 2012-2024 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.springframework.boot.autoconfigure; - -import java.nio.charset.StandardCharsets; -import java.time.ZoneId; -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.atomic.AtomicBoolean; - -import jakarta.validation.Configuration; -import jakarta.validation.Validation; -import org.apache.catalina.authenticator.NonLoginAuthenticator; -import org.apache.tomcat.util.http.Rfc6265CookieProcessor; - -import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent; -import org.springframework.boot.context.event.ApplicationFailedEvent; -import org.springframework.boot.context.event.ApplicationReadyEvent; -import org.springframework.boot.context.event.SpringApplicationEvent; -import org.springframework.boot.context.logging.LoggingApplicationListener; -import org.springframework.context.ApplicationListener; -import org.springframework.core.NativeDetector; -import org.springframework.core.Ordered; -import org.springframework.format.support.DefaultFormattingConversionService; -import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder; -import org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter; - -/** - * {@link ApplicationListener} to trigger early initialization in a background thread of - * time-consuming tasks. - *

- * Set the {@link #IGNORE_BACKGROUNDPREINITIALIZER_PROPERTY_NAME} system property to - * {@code true} to disable this mechanism and let such initialization happen in the - * foreground. - * - * @author Phillip Webb - * @author Andy Wilkinson - * @author Artsiom Yudovin - * @author Sebastien Deleuze - * @since 1.3.0 - */ -public class BackgroundPreinitializer implements ApplicationListener, Ordered { - - /** - * System property that instructs Spring Boot how to run pre initialization. When the - * property is set to {@code true}, no pre-initialization happens and each item is - * initialized in the foreground as it needs to. When the property is {@code false} - * (default), pre initialization runs in a separate thread in the background. - * @since 2.1.0 - */ - public static final String IGNORE_BACKGROUNDPREINITIALIZER_PROPERTY_NAME = "spring.backgroundpreinitializer.ignore"; - - private static final AtomicBoolean preinitializationStarted = new AtomicBoolean(); - - private static final CountDownLatch preinitializationComplete = new CountDownLatch(1); - - private static final boolean ENABLED = !Boolean.getBoolean(IGNORE_BACKGROUNDPREINITIALIZER_PROPERTY_NAME) - && Runtime.getRuntime().availableProcessors() > 1; - - @Override - public int getOrder() { - return LoggingApplicationListener.DEFAULT_ORDER + 1; - } - - @Override - public void onApplicationEvent(SpringApplicationEvent event) { - if (!ENABLED || NativeDetector.inNativeImage()) { - return; - } - if (event instanceof ApplicationEnvironmentPreparedEvent - && preinitializationStarted.compareAndSet(false, true)) { - performPreinitialization(); - } - if ((event instanceof ApplicationReadyEvent || event instanceof ApplicationFailedEvent) - && preinitializationStarted.get()) { - try { - preinitializationComplete.await(); - } - catch (InterruptedException ex) { - Thread.currentThread().interrupt(); - } - } - } - - private void performPreinitialization() { - try { - Thread thread = new Thread(new Runnable() { - - @Override - public void run() { - runSafely(new ConversionServiceInitializer()); - runSafely(new ValidationInitializer()); - if (!runSafely(new MessageConverterInitializer())) { - // If the MessageConverterInitializer fails to run, we still might - // be able to - // initialize Jackson - runSafely(new JacksonInitializer()); - } - runSafely(new CharsetInitializer()); - runSafely(new TomcatInitializer()); - runSafely(new JdkInitializer()); - preinitializationComplete.countDown(); - } - - boolean runSafely(Runnable runnable) { - try { - runnable.run(); - return true; - } - catch (Throwable ex) { - return false; - } - } - - }, "background-preinit"); - thread.start(); - } - catch (Exception ex) { - // This will fail on GAE where creating threads is prohibited. We can safely - // continue but startup will be slightly slower as the initialization will now - // happen on the main thread. - preinitializationComplete.countDown(); - } - } - - /** - * Early initializer for Spring MessageConverters. - */ - private static final class MessageConverterInitializer implements Runnable { - - @Override - public void run() { - new AllEncompassingFormHttpMessageConverter(); - } - - } - - /** - * Early initializer for jakarta.validation. - */ - private static final class ValidationInitializer implements Runnable { - - @Override - public void run() { - Configuration configuration = Validation.byDefaultProvider().configure(); - configuration.buildValidatorFactory().getValidator(); - } - - } - - /** - * Early initializer for Jackson. - */ - private static final class JacksonInitializer implements Runnable { - - @Override - public void run() { - Jackson2ObjectMapperBuilder.json().build(); - } - - } - - /** - * Early initializer for Spring's ConversionService. - */ - private static final class ConversionServiceInitializer implements Runnable { - - @Override - public void run() { - new DefaultFormattingConversionService(); - } - - } - - private static final class CharsetInitializer implements Runnable { - - @Override - public void run() { - StandardCharsets.UTF_8.name(); - } - - } - - private static final class TomcatInitializer implements Runnable { - - @Override - public void run() { - new Rfc6265CookieProcessor(); - new NonLoginAuthenticator(); - } - - } - - private static final class JdkInitializer implements Runnable { - - @Override - public void run() { - ZoneId.systemDefault(); - } - - } - -} diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/preinitialize/BackgroundPreinitializer.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/preinitialize/BackgroundPreinitializer.java new file mode 100644 index 000000000000..f75e805473b1 --- /dev/null +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/preinitialize/BackgroundPreinitializer.java @@ -0,0 +1,35 @@ +/* + * Copyright 2012-2024 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.autoconfigure.preinitialize; + +/** + * Interface used to preinitialize in the background code that may otherwise cause a delay + * when first called. Implementations should be registered in {@code spring.factories}. + * + * @author Phillip Webb + * @since 4.0.0 + */ +@FunctionalInterface +public interface BackgroundPreinitializer { + + /** + * Perform any require preinitialization. + * @throws Exception on any initialization error + */ + void preinitialize() throws Exception; + +} diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/preinitialize/BackgroundPreinitializingApplicationListener.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/preinitialize/BackgroundPreinitializingApplicationListener.java new file mode 100644 index 000000000000..a62ea81f81d9 --- /dev/null +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/preinitialize/BackgroundPreinitializingApplicationListener.java @@ -0,0 +1,133 @@ +/* + * Copyright 2012-2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.autoconfigure.preinitialize; + +import java.util.List; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.atomic.AtomicBoolean; + +import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent; +import org.springframework.boot.context.event.ApplicationFailedEvent; +import org.springframework.boot.context.event.ApplicationReadyEvent; +import org.springframework.boot.context.event.SpringApplicationEvent; +import org.springframework.boot.context.logging.LoggingApplicationListener; +import org.springframework.context.ApplicationListener; +import org.springframework.core.NativeDetector; +import org.springframework.core.Ordered; +import org.springframework.core.io.support.SpringFactoriesLoader; + +/** + * {@link ApplicationListener} to trigger early initialization in a background thread of + * time-consuming tasks. + *

+ * Set the {@link #IGNORE_BACKGROUNDPREINITIALIZER_PROPERTY_NAME} system property to + * {@code true} to disable this mechanism. + * + * @author Phillip Webb + * @author Andy Wilkinson + * @author Artsiom Yudovin + * @author Sebastien Deleuze + * @see BackgroundPreinitializer + */ +class BackgroundPreinitializingApplicationListener implements ApplicationListener, Ordered { + + /** + * System property that instructs Spring Boot how to run pre initialization. When the + * property is set to {@code true}, no pre-initialization happens and each item is + * initialized in the foreground as it needs to. When the property is {@code false} + * (default), pre initialization runs in a separate thread in the background. + */ + public static final String IGNORE_BACKGROUNDPREINITIALIZER_PROPERTY_NAME = "spring.backgroundpreinitializer.ignore"; + + private static final AtomicBoolean started = new AtomicBoolean(); + + private static final CountDownLatch complete = new CountDownLatch(1); + + private final SpringFactoriesLoader factoriesLoader; + + private final boolean enabled; + + BackgroundPreinitializingApplicationListener() { + this(SpringFactoriesLoader.forDefaultResourceLocation()); + } + + BackgroundPreinitializingApplicationListener(SpringFactoriesLoader factoriesLoader) { + this.factoriesLoader = factoriesLoader; + this.enabled = !NativeDetector.inNativeImage() + && !Boolean.getBoolean(IGNORE_BACKGROUNDPREINITIALIZER_PROPERTY_NAME) + && Runtime.getRuntime().availableProcessors() > 1; + } + + @Override + public int getOrder() { + return LoggingApplicationListener.DEFAULT_ORDER + 1; + } + + @Override + public void onApplicationEvent(SpringApplicationEvent event) { + if (!this.enabled) { + return; + } + if (event instanceof ApplicationEnvironmentPreparedEvent && started.compareAndSet(false, true)) { + preinitialize(); + } + if ((event instanceof ApplicationReadyEvent || event instanceof ApplicationFailedEvent) && started.get()) { + try { + complete.await(); + } + catch (InterruptedException ex) { + Thread.currentThread().interrupt(); + } + } + } + + private void preinitialize() { + Runner runner = new Runner(this.factoriesLoader.load(BackgroundPreinitializer.class)); + try { + Thread thread = new Thread(runner, "background-preinit"); + thread.start(); + } + catch (Exception ex) { + // This will fail on Google App Engine where creating threads is + // prohibited. We can safely continue but startup will be slightly slower + // as the initialization will now happen on the main thread. + complete.countDown(); + } + } + + /** + * Runner thread to call the {@link BackgroundPreinitializer} instances. + * + * @param preinitializers the preinitializers + */ + record Runner(List preinitializers) implements Runnable { + + @Override + public void run() { + for (BackgroundPreinitializer preinitializer : this.preinitializers) { + try { + preinitializer.preinitialize(); + } + catch (Throwable ex) { + } + } + complete.countDown(); + } + + } + +} diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/preinitialize/CharsetsBackgroundPreinitializer.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/preinitialize/CharsetsBackgroundPreinitializer.java new file mode 100644 index 000000000000..9834164b39c9 --- /dev/null +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/preinitialize/CharsetsBackgroundPreinitializer.java @@ -0,0 +1,33 @@ +/* + * Copyright 2012-2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.autoconfigure.preinitialize; + +import java.nio.charset.StandardCharsets; + +/** + * {@link BackgroundPreinitializer} for commonly used charsets. + * + * @author Phillip Webb + */ +final class CharsetsBackgroundPreinitializer implements BackgroundPreinitializer { + + @Override + public void preinitialize() throws Exception { + StandardCharsets.UTF_8.name(); + } + +} diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/preinitialize/ConversionServiceBackgroundPreinitializer.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/preinitialize/ConversionServiceBackgroundPreinitializer.java new file mode 100644 index 000000000000..6f430e5c9187 --- /dev/null +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/preinitialize/ConversionServiceBackgroundPreinitializer.java @@ -0,0 +1,34 @@ +/* + * Copyright 2012-2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.autoconfigure.preinitialize; + +import org.springframework.core.convert.ConversionService; +import org.springframework.format.support.DefaultFormattingConversionService; + +/** + * {@link BackgroundPreinitializer} for Spring's {@link ConversionService}. + * + * @author Phillip Webb + */ +final class ConversionServiceBackgroundPreinitializer implements BackgroundPreinitializer { + + @Override + public void preinitialize() throws Exception { + new DefaultFormattingConversionService(); + } + +} diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/preinitialize/JacksonBackgroundPreinitializer.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/preinitialize/JacksonBackgroundPreinitializer.java new file mode 100644 index 000000000000..83a39c01ae09 --- /dev/null +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/preinitialize/JacksonBackgroundPreinitializer.java @@ -0,0 +1,33 @@ +/* + * Copyright 2012-2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.autoconfigure.preinitialize; + +import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder; + +/** + * {@link JacksonBackgroundPreinitializer} for Jackson. + * + * @author Phillip Webb + */ +final class JacksonBackgroundPreinitializer implements BackgroundPreinitializer { + + @Override + public void preinitialize() throws Exception { + Jackson2ObjectMapperBuilder.json().build(); + } + +} diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/preinitialize/JakartaValidationBackgroundPreinitializer.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/preinitialize/JakartaValidationBackgroundPreinitializer.java new file mode 100644 index 000000000000..5ad2bae86763 --- /dev/null +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/preinitialize/JakartaValidationBackgroundPreinitializer.java @@ -0,0 +1,35 @@ +/* + * Copyright 2012-2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.autoconfigure.preinitialize; + +import jakarta.validation.Configuration; +import jakarta.validation.Validation; + +/** + * {@link BackgroundPreinitializer} for jakarta.validation. + * + * @author Phillip Webb + */ +final class JakartaValidationBackgroundPreinitializer implements BackgroundPreinitializer { + + @Override + public void preinitialize() throws Exception { + Configuration configuration = Validation.byDefaultProvider().configure(); + configuration.buildValidatorFactory().getValidator(); + } + +} diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/preinitialize/MessageConverterBackgroundPreinitializer.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/preinitialize/MessageConverterBackgroundPreinitializer.java new file mode 100644 index 000000000000..9a37a1fbf39c --- /dev/null +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/preinitialize/MessageConverterBackgroundPreinitializer.java @@ -0,0 +1,35 @@ +/* + * Copyright 2012-2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.autoconfigure.preinitialize; + +import org.apache.activemq.artemis.spi.core.protocol.MessageConverter; + +import org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter; + +/** + * {@link BackgroundPreinitializer} Spring's {@link MessageConverter} implementations. + * + * @author Phillip Webb + */ +final class MessageConverterBackgroundPreinitializer implements BackgroundPreinitializer { + + @Override + public void preinitialize() throws Exception { + new AllEncompassingFormHttpMessageConverter(); + } + +} diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/preinitialize/TomcatBackgroundPreinitializer.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/preinitialize/TomcatBackgroundPreinitializer.java new file mode 100644 index 000000000000..489ca65155b5 --- /dev/null +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/preinitialize/TomcatBackgroundPreinitializer.java @@ -0,0 +1,35 @@ +/* + * Copyright 2012-2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.autoconfigure.preinitialize; + +import org.apache.catalina.authenticator.NonLoginAuthenticator; +import org.apache.tomcat.util.http.Rfc6265CookieProcessor; + +/** + * {@link BackgroundPreinitializer} for Tomcat. + * + * @author Phillip Webb + */ +final class TomcatBackgroundPreinitializer implements BackgroundPreinitializer { + + @Override + public void preinitialize() throws Exception { + new Rfc6265CookieProcessor(); + new NonLoginAuthenticator(); + } + +} diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/preinitialize/ZoneIdBackgroundPreinitializer.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/preinitialize/ZoneIdBackgroundPreinitializer.java new file mode 100644 index 000000000000..e96004d60d19 --- /dev/null +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/preinitialize/ZoneIdBackgroundPreinitializer.java @@ -0,0 +1,33 @@ +/* + * Copyright 2012-2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.autoconfigure.preinitialize; + +import java.time.ZoneId; + +/** + * {@link BackgroundPreinitializer} for {@link ZoneId}. + * + * @author Phillip Webb + */ +final class ZoneIdBackgroundPreinitializer implements BackgroundPreinitializer { + + @Override + public void preinitialize() throws Exception { + ZoneId.systemDefault(); + } + +} diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/preinitialize/package-info.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/preinitialize/package-info.java new file mode 100644 index 000000000000..66e130f85bf9 --- /dev/null +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/preinitialize/package-info.java @@ -0,0 +1,20 @@ +/* + * Copyright 2012-2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * Capabilities to preinitialize code in the background to improve startup performance. + */ +package org.springframework.boot.autoconfigure.preinitialize; diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories b/spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories index 9eb9419cdbef..ff71b0305d4e 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories @@ -5,7 +5,7 @@ org.springframework.boot.autoconfigure.logging.ConditionEvaluationReportLoggingL # Application Listeners org.springframework.context.ApplicationListener=\ -org.springframework.boot.autoconfigure.BackgroundPreinitializer +org.springframework.boot.autoconfigure.preinitialize.BackgroundPreinitializingApplicationListener # Environment Post Processors org.springframework.boot.env.EnvironmentPostProcessor=\ @@ -21,6 +21,16 @@ org.springframework.boot.autoconfigure.condition.OnBeanCondition,\ org.springframework.boot.autoconfigure.condition.OnClassCondition,\ org.springframework.boot.autoconfigure.condition.OnWebApplicationCondition +# Background Preinitializers +org.springframework.boot.autoconfigure.preinitialize.BackgroundPreinitializer=\ +org.springframework.boot.autoconfigure.preinitialize.CharsetsBackgroundPreinitializer,\ +org.springframework.boot.autoconfigure.preinitialize.ConversionServiceBackgroundPreinitializer,\ +org.springframework.boot.autoconfigure.preinitialize.JacksonBackgroundPreinitializer,\ +org.springframework.boot.autoconfigure.preinitialize.JakartaValidationBackgroundPreinitializer,\ +org.springframework.boot.autoconfigure.preinitialize.MessageConverterBackgroundPreinitializer,\ +org.springframework.boot.autoconfigure.preinitialize.TomcatBackgroundPreinitializer,\ +org.springframework.boot.autoconfigure.preinitialize.ZoneIdBackgroundPreinitializer + # Failure Analyzers org.springframework.boot.diagnostics.FailureAnalyzer=\ org.springframework.boot.autoconfigure.data.redis.RedisUrlSyntaxFailureAnalyzer,\