Skip to content

Commit 1c6471e

Browse files
committed
Register AprLifecycleListener with Server not Context
Fixes gh-28814
1 parent 2394cbf commit 1c6471e

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/tomcat/TomcatReactiveWebServerFactory.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2021 the original author or authors.
2+
* Copyright 2012-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -72,7 +72,9 @@ public class TomcatReactiveWebServerFactory extends AbstractReactiveWebServerFac
7272

7373
private final List<Valve> engineValves = new ArrayList<>();
7474

75-
private List<LifecycleListener> contextLifecycleListeners = getDefaultLifecycleListeners();
75+
private List<LifecycleListener> contextLifecycleListeners = new ArrayList<>();
76+
77+
private List<LifecycleListener> serverLifecycleListeners = getDefaultServerLifecycleListeners();
7678

7779
private Set<TomcatContextCustomizer> tomcatContextCustomizers = new LinkedHashSet<>();
7880

@@ -105,7 +107,7 @@ public TomcatReactiveWebServerFactory(int port) {
105107
super(port);
106108
}
107109

108-
private static List<LifecycleListener> getDefaultLifecycleListeners() {
110+
private static List<LifecycleListener> getDefaultServerLifecycleListeners() {
109111
AprLifecycleListener aprLifecycleListener = new AprLifecycleListener();
110112
return AprLifecycleListener.isAprAvailable() ? new ArrayList<>(Arrays.asList(aprLifecycleListener))
111113
: new ArrayList<>();
@@ -119,6 +121,9 @@ public WebServer getWebServer(HttpHandler httpHandler) {
119121
Tomcat tomcat = new Tomcat();
120122
File baseDir = (this.baseDirectory != null) ? this.baseDirectory : createTempDir("tomcat");
121123
tomcat.setBaseDir(baseDir.getAbsolutePath());
124+
for (LifecycleListener listener : this.serverLifecycleListeners) {
125+
tomcat.getServer().addLifecycleListener(listener);
126+
}
122127
Connector connector = new Connector(this.protocol);
123128
connector.setThrowOnFailure(true);
124129
tomcat.getService().addConnector(connector);

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/tomcat/TomcatServletWebServerFactory.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2021 the original author or authors.
2+
* Copyright 2012-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -117,7 +117,9 @@ public class TomcatServletWebServerFactory extends AbstractServletWebServerFacto
117117

118118
private List<Valve> contextValves = new ArrayList<>();
119119

120-
private List<LifecycleListener> contextLifecycleListeners = getDefaultLifecycleListeners();
120+
private List<LifecycleListener> contextLifecycleListeners = new ArrayList<>();
121+
122+
private List<LifecycleListener> serverLifecycleListeners = getDefaultServerLifecycleListeners();
121123

122124
private Set<TomcatContextCustomizer> tomcatContextCustomizers = new LinkedHashSet<>();
123125

@@ -166,7 +168,7 @@ public TomcatServletWebServerFactory(String contextPath, int port) {
166168
super(contextPath, port);
167169
}
168170

169-
private static List<LifecycleListener> getDefaultLifecycleListeners() {
171+
private static List<LifecycleListener> getDefaultServerLifecycleListeners() {
170172
ArrayList<LifecycleListener> lifecycleListeners = new ArrayList<>();
171173
if (!NativeDetector.inNativeImage()) {
172174
AprLifecycleListener aprLifecycleListener = new AprLifecycleListener();
@@ -185,6 +187,9 @@ public WebServer getWebServer(ServletContextInitializer... initializers) {
185187
Tomcat tomcat = new Tomcat();
186188
File baseDir = (this.baseDirectory != null) ? this.baseDirectory : createTempDir("tomcat");
187189
tomcat.setBaseDir(baseDir.getAbsolutePath());
190+
for (LifecycleListener listener : this.serverLifecycleListeners) {
191+
tomcat.getServer().addLifecycleListener(listener);
192+
}
188193
Connector connector = new Connector(this.protocol);
189194
connector.setThrowOnFailure(true);
190195
tomcat.getService().addConnector(connector);

0 commit comments

Comments
 (0)