From a0a39abb49d0f18a873e3bffd67ab140fce7856b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Nicoll?= Date: Mon, 26 May 2025 16:32:42 +0200 Subject: [PATCH] Introduce a public constant for the WebServerStartStopLifecycle's phase This commit also moves the constant for the phase of WebServerGracefulShutdownLifecycle to WebServerApplicationContext. Closes gh-45572 --- .../ChildManagementContextInitializer.java | 5 ++--- .../context/WebServerApplicationContext.java | 17 ++++++++++++++++- .../WebServerGracefulShutdownLifecycle.java | 7 +++++-- .../context/WebServerStartStopLifecycle.java | 6 +++--- .../context/WebServerStartStopLifecycle.java | 6 +++--- 5 files changed, 29 insertions(+), 12 deletions(-) diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/server/ChildManagementContextInitializer.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/server/ChildManagementContextInitializer.java index 21de0fcad0..0bf5ce431c 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/server/ChildManagementContextInitializer.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/server/ChildManagementContextInitializer.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2024 the original author or authors. + * 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. @@ -34,7 +34,6 @@ import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoCon import org.springframework.boot.context.event.ApplicationFailedEvent; import org.springframework.boot.web.context.ConfigurableWebServerApplicationContext; import org.springframework.boot.web.context.WebServerApplicationContext; -import org.springframework.boot.web.context.WebServerGracefulShutdownLifecycle; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextInitializer; import org.springframework.context.ApplicationEvent; @@ -115,7 +114,7 @@ class ChildManagementContextInitializer implements BeanRegistrationAotProcessor, @Override public int getPhase() { - return WebServerGracefulShutdownLifecycle.SMART_LIFECYCLE_PHASE - 512; + return WebServerApplicationContext.GRACEFUL_SHUTDOWN_PHASE - 512; } @Override diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/context/WebServerApplicationContext.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/context/WebServerApplicationContext.java index c37d96092a..8ee894e8d7 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/context/WebServerApplicationContext.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/context/WebServerApplicationContext.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2023 the original author or authors. + * 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. @@ -18,6 +18,7 @@ package org.springframework.boot.web.context; import org.springframework.boot.web.server.WebServer; import org.springframework.context.ApplicationContext; +import org.springframework.context.SmartLifecycle; import org.springframework.util.ObjectUtils; /** @@ -29,6 +30,20 @@ import org.springframework.util.ObjectUtils; */ public interface WebServerApplicationContext extends ApplicationContext { + /** + * {@link SmartLifecycle#getPhase() SmartLifecycle phase} in which graceful shutdown + * of the web server is performed. + * @since 4.0.0 + */ + int GRACEFUL_SHUTDOWN_PHASE = SmartLifecycle.DEFAULT_PHASE - 1024; + + /** + * {@link SmartLifecycle#getPhase() SmartLifecycle phase} in which starting and + * stopping of the web server is performed. + * @since 4.0.0 + */ + int START_STOP_LIFECYCLE_PHASE = GRACEFUL_SHUTDOWN_PHASE - 1024; + /** * Returns the {@link WebServer} that was created by the context or {@code null} if * the server has not yet been created. diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/context/WebServerGracefulShutdownLifecycle.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/context/WebServerGracefulShutdownLifecycle.java index 35d0fd290b..0a4f57515f 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/context/WebServerGracefulShutdownLifecycle.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/context/WebServerGracefulShutdownLifecycle.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2022 the original author or authors. + * 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. @@ -30,7 +30,10 @@ public final class WebServerGracefulShutdownLifecycle implements SmartLifecycle /** * {@link SmartLifecycle#getPhase() SmartLifecycle phase} in which graceful shutdown * of the web server is performed. + * @deprecated as of 4.0.0 in favor of + * {@link WebServerApplicationContext#GRACEFUL_SHUTDOWN_PHASE} */ + @Deprecated(since = "4.0.0", forRemoval = true) public static final int SMART_LIFECYCLE_PHASE = SmartLifecycle.DEFAULT_PHASE - 1024; private final WebServer webServer; @@ -69,7 +72,7 @@ public final class WebServerGracefulShutdownLifecycle implements SmartLifecycle @Override public int getPhase() { - return SMART_LIFECYCLE_PHASE; + return WebServerApplicationContext.GRACEFUL_SHUTDOWN_PHASE; } } diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/reactive/context/WebServerStartStopLifecycle.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/reactive/context/WebServerStartStopLifecycle.java index 9d1406f2ee..28012ac63e 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/reactive/context/WebServerStartStopLifecycle.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/reactive/context/WebServerStartStopLifecycle.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2022 the original author or authors. + * 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. @@ -16,7 +16,7 @@ package org.springframework.boot.web.reactive.context; -import org.springframework.boot.web.context.WebServerGracefulShutdownLifecycle; +import org.springframework.boot.web.context.WebServerApplicationContext; import org.springframework.boot.web.server.WebServer; import org.springframework.context.SmartLifecycle; @@ -55,7 +55,7 @@ class WebServerStartStopLifecycle implements SmartLifecycle { @Override public int getPhase() { - return WebServerGracefulShutdownLifecycle.SMART_LIFECYCLE_PHASE - 1024; + return WebServerApplicationContext.START_STOP_LIFECYCLE_PHASE; } } diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/context/WebServerStartStopLifecycle.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/context/WebServerStartStopLifecycle.java index 4823211ba9..05c4518442 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/context/WebServerStartStopLifecycle.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/context/WebServerStartStopLifecycle.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2023 the original author or authors. + * 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. @@ -16,7 +16,7 @@ package org.springframework.boot.web.servlet.context; -import org.springframework.boot.web.context.WebServerGracefulShutdownLifecycle; +import org.springframework.boot.web.context.WebServerApplicationContext; import org.springframework.boot.web.server.WebServer; import org.springframework.context.SmartLifecycle; @@ -60,7 +60,7 @@ class WebServerStartStopLifecycle implements SmartLifecycle { @Override public int getPhase() { - return WebServerGracefulShutdownLifecycle.SMART_LIFECYCLE_PHASE - 1024; + return WebServerApplicationContext.START_STOP_LIFECYCLE_PHASE; } }