diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/tomcat/TomcatReactiveWebServerFactory.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/tomcat/TomcatReactiveWebServerFactory.java index 8696ac6f82..ca4a48a83c 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/tomcat/TomcatReactiveWebServerFactory.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/tomcat/TomcatReactiveWebServerFactory.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2021 the original author or authors. + * Copyright 2012-2022 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. @@ -72,7 +72,9 @@ public class TomcatReactiveWebServerFactory extends AbstractReactiveWebServerFac private final List engineValves = new ArrayList<>(); - private List contextLifecycleListeners = getDefaultLifecycleListeners(); + private List contextLifecycleListeners = new ArrayList<>(); + + private List serverLifecycleListeners = getDefaultServerLifecycleListeners(); private Set tomcatContextCustomizers = new LinkedHashSet<>(); @@ -105,7 +107,7 @@ public class TomcatReactiveWebServerFactory extends AbstractReactiveWebServerFac super(port); } - private static List getDefaultLifecycleListeners() { + private static List getDefaultServerLifecycleListeners() { AprLifecycleListener aprLifecycleListener = new AprLifecycleListener(); return AprLifecycleListener.isAprAvailable() ? new ArrayList<>(Arrays.asList(aprLifecycleListener)) : new ArrayList<>(); @@ -119,6 +121,9 @@ public class TomcatReactiveWebServerFactory extends AbstractReactiveWebServerFac Tomcat tomcat = new Tomcat(); File baseDir = (this.baseDirectory != null) ? this.baseDirectory : createTempDir("tomcat"); tomcat.setBaseDir(baseDir.getAbsolutePath()); + for (LifecycleListener listener : this.serverLifecycleListeners) { + tomcat.getServer().addLifecycleListener(listener); + } Connector connector = new Connector(this.protocol); connector.setThrowOnFailure(true); tomcat.getService().addConnector(connector); diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/tomcat/TomcatServletWebServerFactory.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/tomcat/TomcatServletWebServerFactory.java index 9e1bced220..3c4db9298f 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/tomcat/TomcatServletWebServerFactory.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/tomcat/TomcatServletWebServerFactory.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2021 the original author or authors. + * Copyright 2012-2022 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. @@ -124,7 +124,9 @@ public class TomcatServletWebServerFactory extends AbstractServletWebServerFacto private List contextValves = new ArrayList<>(); - private List contextLifecycleListeners = getDefaultLifecycleListeners(); + private List contextLifecycleListeners = new ArrayList<>(); + + private List serverLifecycleListeners = getDefaultServerLifecycleListeners(); private Set tomcatContextCustomizers = new LinkedHashSet<>(); @@ -173,7 +175,7 @@ public class TomcatServletWebServerFactory extends AbstractServletWebServerFacto super(contextPath, port); } - private static List getDefaultLifecycleListeners() { + private static List getDefaultServerLifecycleListeners() { ArrayList lifecycleListeners = new ArrayList<>(); if (!NativeDetector.inNativeImage()) { AprLifecycleListener aprLifecycleListener = new AprLifecycleListener(); @@ -192,6 +194,9 @@ public class TomcatServletWebServerFactory extends AbstractServletWebServerFacto Tomcat tomcat = new Tomcat(); File baseDir = (this.baseDirectory != null) ? this.baseDirectory : createTempDir("tomcat"); tomcat.setBaseDir(baseDir.getAbsolutePath()); + for (LifecycleListener listener : this.serverLifecycleListeners) { + tomcat.getServer().addLifecycleListener(listener); + } Connector connector = new Connector(this.protocol); connector.setThrowOnFailure(true); tomcat.getService().addConnector(connector);