0

在 Tomcat 6 的lib( ${catalina.home}/lib/) 文件夹中,我有几个共享的 JAR 文件,例如 Hibernate 的。

当我调试我的 Web 应用程序时,控制台会通过 Hibernate 获得大量输出,除非我logback.xml输入${catalina.home}/lib/.

这是问题的重点:
如果我将其移动logback.xml到我的网络应用程序的/WEB-INF/lib/文件夹中,它将被忽略,就好像没有文件一样 - 我得到了所有的输出。

这显然与上下文有关(在我假设的 Java 级别上),但我找不到有关它的信息。
请帮助我更好地理解它,基本原理。

谢谢!

4

1 回答 1

3

I bet it isn't related with the context but rather the classpath hierarchy. Java infrastructure provides a mechanism (ClassLoader) to build classpath hierarchies and Tomcat (as many other servlet containers/application servers) use that to isolate the different JARs/class folders being used in a specific application.

The root class loader is the one that has less visibility inside the hierarchy, nested class loaders can look for a class inside their scope or ask a parent class loader for it if not found. Some application servers allow to configure the class loaders to ask first the parent and then look inside their own scope if the parent can't locate it. However, a parent class loader can't never load a class from a nested class loader.

NOTE: same happens with normal files, as with your logback.xml.

So, you have some hibernate libraries deployed in your Tomcat's lib folder, which is handled by the root class loader in the hierarchy. When you have your logback.xml file in that folder it actually is at the same classpath hierarchy level than your Hibernate JARs, so Hibernate (or the log mechanism being used by it) can load the file because it's within its scope.

In the other hand, the libraries used by your application (WEB-INF/lib) are handled by a different class loader which actually is nested to the previous mentioned one. When you move your logback.xml to your application's library folders you are actually moving it to a wider scope but, since Hibernate has been loaded by a parent class loader, it can not locate the file within its scope (remember, a parent class loader can't load classes or files from its children, just the children can ask the parent for those).

于 2012-08-15T14:57:08.513 回答