[英]/WEB-INF/classes vs /WEB-INF/lib
I'd like to package my Java EE6
web classes (beans, filters, servlets) into jar and place it into /WEB-INF/lib/
directory along with other utility jars and abandon /WEB-INF/classes/
directory totally. 我想将
Java EE6
Web类(bean,过滤器,servlet)打包到jar中,并将其与其他实用程序jar放在/WEB-INF/lib/
目录中,并完全放弃/WEB-INF/classes/
目录。
Are there any substantial differences between the two in terms of classloading, acessing application context, etc? 在类加载,访问应用程序上下文等方面,两者之间是否存在实质性差异?
Thanks. 谢谢。
PS: Whenever googling any of java specs I'm always redirected to Oracle documentation index which is dozen clicks away from original url. PS:每当谷歌搜索任何java规范时,我总是被重定向到Oracle文档索引,这是远离原始URL的十几次点击。 Anyone knows what's happening there?
谁知道那里发生了什么?
I'd go for /WEB-INF/classes
. 我会去
/WEB-INF/classes
。 It allows you to run your application in debug mode and hot-swap classes on change. 它允许您在调试模式下运行应用程序,在更改时运行热交换类。 If you package everything as a jar, you'd have to repackage and redeploy the app every time you change a class.
如果您将所有内容打包为jar,则每次更改类时都必须重新打包并重新部署应用程序。
Well, shortly: Imagine you have class org.example.Test.class
, if you put it into jar and in WEB-INF/lib/
directory, and copy the same class into WEB-INF/classes/
then classloader of that application will use last one (from WEB-INF/classes/
). 好吧,很快:想象一下你有类
org.example.Test.class
,如果你将它放入jar和WEB-INF/lib/
目录中,并将同一个类复制到WEB-INF/classes/
然后该应用程序的类加载器将使用最后一个(来自WEB-INF/classes/
)。
Sometimes you can use it as advantage - I have a library, and it has a bug... I look for source of that class (where bug is; I miss the part of how I know that bug is in that class, that's another story), I add that class to the project with fixed code, and it is compiled into WEB-INF/classes/
while library still exist in WEB-INF/lib/
. 有时你可以使用它作为优势 - 我有一个库,它有一个bug ...我寻找那个类的来源(bug在哪里;我想念我知道那个bug在那个类中的部分,那是另一个故事),我用固定代码将该类添加到项目中,并将其编译为
WEB-INF/classes/
而WEB-INF/lib/
仍存在于WEB-INF/lib/
。 Fixed class will be used until library will be fixed. 固定类将被使用,直到库被修复。
In Tomcat Servlet container's definition: WEB-INF\\classes
is searched before WEB-INF\\lib
. 在Tomcat Servlet容器的定义中:在
WEB-INF\\lib
之前搜索WEB-INF\\classes
。 You can choose to delegate your classloading to your custom classloader - even then the order above is maintained. 您可以选择将类加载委托给自定义类加载器 - 即使这样,上面的顺序也会保留。
If you choose to go with a different provider eg JBOss, Glassfish, Jetty it might have a different order, but I am not sure about those. 如果您选择使用不同的提供商,例如JBOss,Glassfish,Jetty,它可能会有不同的订单,但我不确定。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.