Java Web 应用程序中在类路径中包含资源的最常见标准是什么?

发布于 2024-10-04 09:02:48 字数 200 浏览 4 评论 0原文

我正在使用 Spring 国际化,并且属性文件需要位于类路径上。我还有一些 XML 文件需要位于类路径中。是否可以只将这些资源包含在子目录的“src”中,然后让它们构建到类路径中,或者在启动期间向类路径中添加不同的文件夹更好?我正在使用 Ant,但从它的外观来看,这是 Maven 采取的方法(src 或 test 下的所有内容)。我正在寻找最广泛接受的行业标准或更好的替代方案。谢谢!

I'm using internationalization with Spring, and the properties file needs to be on the classpath. I also have some XML files that will need to be on the classpath. Is it acceptable to just include those resources inside the "src" in a sub-directory, and then let them build to the classpath, or is it better to add a different folder to the classpath during startup? I'm using Ant, but from the looks of it this was the approach Maven took (everything under src or test). I'm looking for the most widely accepted industry standards or better alternatives. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

初相遇 2024-10-11 09:02:48

仅将这些资源包含在子目录的“src”中,然后让它们构建到类路径中是否可以接受

是否可以接受取决于相关资源的唯一用途。使用这种方法,此类资源文件中的任何细微编辑都需要完全重建、重新部署和重新启动。

这可能不一定会损害一次性读取的启动和应用程序范围的配置文件,例如 web.xmlapplication.xml 以及 consorts,因为这通常会影响(或受) Java 源代码中的更改无论如何都需要完全重建/重新部署/等。

但是,对于像 i18n 属性文件和特定于环境的配置文件这样的运行时文件(可以由服务器管理员或客户等非开发人员管理),将其打包到 Web 应用程序中是没有用的。这需要了解如何在编辑后重建 Web 应用程序。您更愿意将其外部化,以便只需要重新启动 Web 应用程序即可反映配置中的更改,或者甚至根本不需要,就像 ResourceBundle 一样,它只会自动重新加载。

我自己通常将这些文件放在 servletcontainer 的固定路径中,并将该路径添加到 servletcontainer 的运行时类路径中。例如,对于 Tomcat,它可以配置为 /conf/catalina.properties 中的 shared.loader 属性。例如

shared.loader=/var/webapp

,此文件夹中的任何内容都会被放入 servletcontainer 的(和 webapp 的)运行时类路径中。

Is it acceptable to just include those resources inside the "src" in a sub-directory, and then let them build to the classpath

Depends on the sole purpose of the resource in question. With this approach, any minor edit in such a resource file would thus require a full rebuild, redeploy and restart.

This may not necessarily harm for one-time-read startup and applicationwide configuration files like web.xml and application.xml and consorts since that would usually affect (or be affected by) changes in Java source code which require a full rebuild/redeploy/etc anyway.

But in case of runtime files like i18n properties files and environment-specific configuration files (which would/could be managed by a non-developer like a serveradmin or a customer), it is not useful to package it inside the webapplication. This requires knowledge how to rebuild the webapp after edits. You would rather like to externalize it so that only a webapp restart is required to reflect the changes in the configuration, or maybe even not at all, like for ResourceBundle which will just reload automagically.

I myself usually put such files in a fixed path along the servletcontainer and add that path to the servletcontainer's runtime classpath. In case of for example Tomcat, it's configureable as shared.loader property in /conf/catalina.properties. E.g.

shared.loader=/var/webapp

Anything in this folder is then taken in the servletcontainer's (and webapp's) runtime classpath.

放低过去 2024-10-11 09:02:48

您放入 WEB-INF/classes 目录中的任何内容都会自动添加到 CLASSPATH 中。

我通常只将 .java 文件放在 /src/test 目录下。我放在其他地方的任何资源都必须放在 WEB-INF/classes 中。创建 WAR 文件时,我的 IDE (IntelliJ) 或 Ant 将它们放在那里。

我建议遵循 Spring 示例并将资源放在适当的位置。

例如,如果您使用 Velocity 作为模板引擎,您将看到 Spring 配置允许您将它们放在 /WEB-INF/vm_views 下。

属性放置在 WEB-INF/classes 中。

检查 Spring 文档以获取示例。

Anything you put in your WEB-INF/classes directory is automatically in the CLASSPATH.

I usually put only .java files under /src and /test directories. Any resources that I put elsewhere have to end up in WEB-INF/classes. It's either my IDE (IntelliJ) or Ant that put them there when the WAR file is created.

I would recommend following the Spring examples and put resources where they do.

For example, if you use Velocity as your templating engine, you'll see that Spring configuration allows you to put them under /WEB-INF/vm_views.

Properties are put in WEB-INF/classes.

Check the Spring docs for examples.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文