diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/jetty/JettyWebServer.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/jetty/JettyWebServer.java index 7661ed9bcd..5bb4f182f2 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/jetty/JettyWebServer.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/jetty/JettyWebServer.java @@ -154,9 +154,8 @@ public class JettyWebServer implements WebServer { } } this.started = true; - JettyWebServer.logger - .info("Jetty started on port(s) " + getActualPortsDescription() - + " with context path '" + getContextPath() + "'"); + logger.info("Jetty started on port(s) " + getActualPortsDescription() + + " with context path '" + getContextPath() + "'"); } catch (WebServerException ex) { stopSilently(); @@ -188,8 +187,7 @@ public class JettyWebServer implements WebServer { connector); } catch (Exception ex) { - JettyWebServer.logger - .info("could not determine port ( " + ex.getMessage() + ")"); + logger.info("could not determine port ( " + ex.getMessage() + ")"); return 0; } } diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/netty/NettyWebServer.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/netty/NettyWebServer.java index 2f921372bf..5dfd74fe0f 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/netty/NettyWebServer.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/netty/NettyWebServer.java @@ -74,7 +74,7 @@ public class NettyWebServer implements WebServer { } throw new WebServerException("Unable to start Netty", ex); } - NettyWebServer.logger.info("Netty started on port(s): " + getPort()); + logger.info("Netty started on port(s): " + getPort()); startDaemonAwaitThread(this.disposableServer); } } diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/tomcat/TomcatEmbeddedContext.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/tomcat/TomcatEmbeddedContext.java index 8276cd674f..9384c259fc 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/tomcat/TomcatEmbeddedContext.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/tomcat/TomcatEmbeddedContext.java @@ -51,19 +51,26 @@ class TomcatEmbeddedContext extends StandardContext { } public void deferredLoadOnStartup() throws LifecycleException { - // Some older Servlet frameworks (e.g. Struts, BIRT) use the Thread context class - // loader to create servlet instances in this phase. If they do that and then try - // to initialize them later the class loader may have changed, so wrap the call to - // loadOnStartup in what we think its going to be the main webapp classloader at - // runtime. - ClassLoader classLoader = getLoader().getClassLoader(); - ClassLoader existingLoader = null; - if (classLoader != null) { - existingLoader = ClassUtils.overrideThreadContextClassLoader(classLoader); - } - try { + doWithThreadContextClassLoader(getLoader().getClassLoader(), () -> { boolean started = super.loadOnStartup(findChildren()); Assert.state(started, "Unable to start embedded tomcat context " + getName()); + }); + } + + /** + * Some older Servlet frameworks (e.g. Struts, BIRT) use the Thread context class + * loader to create servlet instances in this phase. If they do that and then try to + * initialize them later the class loader may have changed, so wrap the call to + * loadOnStartup in what we think its going to be the main webapp classloader at + * runtime. + * @param classLoader the class loader to use + * @param code the code to run + */ + private void doWithThreadContextClassLoader(ClassLoader classLoader, Runnable code) { + ClassLoader existingLoader = (classLoader != null) + ? ClassUtils.overrideThreadContextClassLoader(classLoader) : null; + try { + code.run(); } finally { if (existingLoader != null) { diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/tomcat/TomcatWebServer.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/tomcat/TomcatWebServer.java index 8da442eb2f..75d67d781e 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/tomcat/TomcatWebServer.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/tomcat/TomcatWebServer.java @@ -87,8 +87,7 @@ public class TomcatWebServer implements WebServer { } private void initialize() throws WebServerException { - TomcatWebServer.logger - .info("Tomcat initialized with port(s): " + getPortsDescription(false)); + logger.info("Tomcat initialized with port(s): " + getPortsDescription(false)); synchronized (this.monitor) { try { addInstanceIdToEngineName(); @@ -202,9 +201,8 @@ public class TomcatWebServer implements WebServer { } checkThatConnectorsHaveStarted(); this.started = true; - TomcatWebServer.logger - .info("Tomcat started on port(s): " + getPortsDescription(true) - + " with context path '" + getContextPath() + "'"); + logger.info("Tomcat started on port(s): " + getPortsDescription(true) + + " with context path '" + getContextPath() + "'"); } catch (ConnectorStartFailedException ex) { stopSilently(); @@ -273,7 +271,7 @@ public class TomcatWebServer implements WebServer { connector.getProtocolHandler().stop(); } catch (Exception ex) { - TomcatWebServer.logger.error("Cannot pause connector: ", ex); + logger.error("Cannot pause connector: ", ex); } } @@ -286,7 +284,7 @@ public class TomcatWebServer implements WebServer { } } catch (Exception ex) { - TomcatWebServer.logger.error("Cannot start connector: ", ex); + logger.error("Cannot start connector: ", ex); throw new WebServerException("Unable to start embedded Tomcat connectors", ex); } diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/undertow/UndertowWebServer.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/undertow/UndertowWebServer.java index 4f56d7847e..b65bfab38c 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/undertow/UndertowWebServer.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/undertow/UndertowWebServer.java @@ -101,8 +101,7 @@ public class UndertowWebServer implements WebServer { } this.undertow.start(); this.started = true; - UndertowWebServer.logger - .info("Undertow started on port(s) " + getPortsDescription()); + logger.info("Undertow started on port(s) " + getPortsDescription()); } catch (Exception ex) { try {