From b4aaeaac7e5862dee254d72e063f3edf23e526a5 Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Thu, 12 Jun 2014 15:10:53 +0100 Subject: [PATCH] Set Thread context class loader while Tomcat starts up Fixes gh-1085 --- .../embedded/tomcat/TomcatEmbeddedContext.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/spring-boot/src/main/java/org/springframework/boot/context/embedded/tomcat/TomcatEmbeddedContext.java b/spring-boot/src/main/java/org/springframework/boot/context/embedded/tomcat/TomcatEmbeddedContext.java index 68ce1c493a..b790682f4c 100644 --- a/spring-boot/src/main/java/org/springframework/boot/context/embedded/tomcat/TomcatEmbeddedContext.java +++ b/spring-boot/src/main/java/org/springframework/boot/context/embedded/tomcat/TomcatEmbeddedContext.java @@ -18,6 +18,7 @@ package org.springframework.boot.context.embedded.tomcat; import org.apache.catalina.Container; import org.apache.catalina.core.StandardContext; +import org.springframework.util.ClassUtils; /** * Tomcat {@link StandardContext} used by {@link TomcatEmbeddedServletContainer} to @@ -32,7 +33,20 @@ class TomcatEmbeddedContext extends StandardContext { } public void deferredLoadOnStartup() { + // 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); + } super.loadOnStartup(findChildren()); + if (existingLoader != null) { + ClassUtils.overrideThreadContextClassLoader(existingLoader); + } } }