java /WEB-INF/classes 与 /WEB-INF/lib

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4739302/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 07:48:52  来源:igfitidea点击:

/WEB-INF/classes vs /WEB-INF/lib

javaweb-applicationsjakarta-ee

提问by andbi

I'd like to package my Java EE6web 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 EE6Web 类(bean、过滤器、servlet)打包到 jar 中,并将其/WEB-INF/lib/与其他实用程序 jar 一起放入目录中,并/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. Anyone knows what's happening there?

PS:每当谷歌搜索任何 Java 规范时,我总是被重定向到 Oracle 文档索引,该索引与原始 url 相距数十次点击。有谁知道那里发生了什么?

回答by Bozho

I'd go for /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.

我会去的/WEB-INF/classes。它允许您在调试模式下运行您的应用程序,并在更改时热插拔类。如果将所有内容都打包为 jar,则每次更改类时都必须重新打包和重新部署应用程序。

回答by Maxym

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/).

好吧,很快:想象一下你有 class 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/. Fixed class will be used until library will be fixed.

有时你可以将它用作优势 - 我有一个库,它有一个错误......我寻找那个类的源(错误在哪里;我错过了我如何知道那个错误在那个类中的部分,那是另一个story),我把那个类添加到项目中,代码固定,WEB-INF/classes/在库仍然存在的时候编译成WEB-INF/lib/. 固定类将被使用,直到库被修复。

回答by ha9u63ar

In Tomcat Servlet container's definition: WEB-INF\classesis searched before WEB-INF\lib. You can choose to delegate your classloading to your custom classloader - even then the order above is maintained.

在 Tomcat Servlet 容器的定义中:WEB-INF\classesWEB-INF\lib. 您可以选择将您的类加载委托给您的自定义类加载器 - 即使如此,上面的顺序也会保持不变。

If you choose to go with a different provider e.g. JBOss, Glassfish, Jetty it might have a different order, but I am not sure about those.

如果您选择使用不同的提供商,例如 JBOss、Glassfish、Jetty,它可能会有不同的顺序,但我不确定这些。