From 9eed719c44352b5f3d2fe10d6c9456408b91c880 Mon Sep 17 00:00:00 2001 From: Brian Clozel Date: Tue, 24 Mar 2020 23:11:18 +0100 Subject: [PATCH] Do not register shutdownHook for WAR deployments The application context shutdownHook is not needed for WAR deployments, and we should let the Servlet container handle the application lifecycle here. Closes gh-19398 --- .../web/servlet/support/SpringBootServletInitializer.java | 3 ++- .../support/SpringBootServletInitializerTests.java | 8 +++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/support/SpringBootServletInitializer.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/support/SpringBootServletInitializer.java index f4820723cc..f443ec5f2c 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/support/SpringBootServletInitializer.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/support/SpringBootServletInitializer.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2020 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. @@ -131,6 +131,7 @@ public abstract class SpringBootServletInitializer implements WebApplicationInit if (this.registerErrorPageFilter) { application.addPrimarySources(Collections.singleton(ErrorPageFilterConfiguration.class)); } + application.setRegisterShutdownHook(false); return run(application); } diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/support/SpringBootServletInitializerTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/support/SpringBootServletInitializerTests.java index d4be91bfe1..790914bc28 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/support/SpringBootServletInitializerTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/support/SpringBootServletInitializerTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2020 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. @@ -100,6 +100,12 @@ class SpringBootServletInitializerTests { WithConfigurationAnnotation.class); } + @Test + void shutdownHookIsNotRegistered() { + new WithConfigurationAnnotation().createRootApplicationContext(this.servletContext); + assertThat(this.application).hasFieldOrPropertyWithValue("registerShutdownHook", false); + } + @Test void errorPageFilterRegistrationCanBeDisabled() { WebServer webServer = new UndertowServletWebServerFactory(0).getWebServer((servletContext) -> {