From 1c6471ef60e7f3de7dc2ef66266895e67b382a47 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Tue, 18 Jan 2022 20:34:08 +0000 Subject: [PATCH] Register AprLifecycleListener with Server not Context Fixes gh-28814 --- .../tomcat/TomcatReactiveWebServerFactory.java | 11 ++++++++--- .../tomcat/TomcatServletWebServerFactory.java | 11 ++++++++--- 2 files changed, 16 insertions(+), 6 deletions(-) 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 35b3b7383a..3cd2cc552e 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. @@ -117,7 +117,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<>(); @@ -166,7 +168,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(); @@ -185,6 +187,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);