战争部署到Tomcat 8.5.27。在此部署期间,实体类将动态添加到类路径上的persistence.xml
文件中。
然后要求Hibernate 5.1.2加载包含这些实体类的持久性单元。它们被添加到java.net.FactoryURLClassLoader
实例中。以下是部署日志的摘录,显示了此类加载器的转储:
[INFO] [talledLocalContainer] 15-Feb-2019 15:03:46.722 INFOS [localhost-startStop-1] org.apache.catalina.core.ApplicationContext.log ** SEARCHING: generated.com.company.Baz
[INFO] [talledLocalContainer] 15-Feb-2019 15:03:46.727 INFOS [localhost-startStop-1] org.apache.catalina.core.ApplicationContext.log ClassLoader: java.net.FactoryURLClassLoader@701f7082
[INFO] [talledLocalContainer] class generated.com.company.Foo
[INFO] [talledLocalContainer] class generated.com.company.Bar
(...)
[INFO] [talledLocalContainer] class generated.com.company.GooClassLoader: ParallelWebappClassLoader
[INFO] [talledLocalContainer] context: webappContext
[INFO] [talledLocalContainer] delegate: false
[INFO] [talledLocalContainer] ----------> Parent Classloader:
[INFO] [talledLocalContainer] java.net.URLClassLoader@45d6cf48
[INFO] [talledLocalContainer]
[INFO] [talledLocalContainer] class com.sun.jersey.server.impl.container.servlet.JerseyServletContainerInitializer
[INFO] [talledLocalContainer] class javax.ws.rs.core.Application
(...)
请注意,以上所有操作似乎都是由localhost-startStop-1
线程执行的。
稍后,当请求到达webapp时,处理该请求的线程(http-nio-8080-exec-6
参见下面的日志)无法在generated.com.company
下找到类。以下是此线程类加载器转储的摘录:
[INFO] [talledLocalContainer] 15-Feb-2019 15:04:18.705 INFOS [http-nio-8080-exec-6] org.apache.catalina.core.ApplicationContext.log ** SEARCHING: generated.com.company.Foo
[INFO] [talledLocalContainer] 15-Feb-2019 15:04:18.724 INFOS [http-nio-8080-exec-6] org.apache.catalina.core.ApplicationContext.log ClassLoader: ParallelWebappClassLoader
[INFO] [talledLocalContainer] context: webappContext
[INFO] [talledLocalContainer] delegate: false
[INFO] [talledLocalContainer] ----------> Parent Classloader:
[INFO] [talledLocalContainer] java.net.URLClassLoader@45d6cf48
[INFO] [talledLocalContainer]
[INFO] [talledLocalContainer] class com.sun.jersey.server.impl.container.servlet.JerseyServletContainerInitializer
[INFO] [talledLocalContainer] class javax.ws.rs.core.Application
(...)
以下是用于生成转储的代码:
private static void dumpLoadedClasses() {
try {
ClassLoader myCL = Thread.currentThread().getContextClassLoader();
StringBuilder sb = new StringBuilder();
while (myCL != null) {
sb.append("ClassLoader: " + myCL);
for (Iterator<Class<?>> iter = list(myCL); iter.hasNext();) {
sb.append("\n\t" + iter.next());
}
myCL = myCL.getParent();
}
log.info(sb.toString());
} catch (Exception e) {
log.warn("", e);
}
}
为什么http-nio-8080-exec-6
没有java.net.FactoryURLClassLoader@701f7082
作为他的上下文类加载器?
考虑使用ClassLoader.getParent(),也许他们有共同的祖先