Set Thread context class loader while Tomcat starts up

Fixes gh-1085
This commit is contained in:
Dave Syer 2014-06-12 15:10:53 +01:00
parent c0efd3a22e
commit b4aaeaac7e
1 changed files with 14 additions and 0 deletions

View File

@ -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);
}
}
}