tomcat 的 lib 中的 jar 在我的 web 应用程序中看不到属性文件
最近,我将核心 servlet 应用程序的核心功能分离到一个 jar 文件中。该 jar 文件现在部署在 tomcat 的 lib 文件夹中,并且应用程序(即 servlet、jsps、属性文件等)作为 war 文件独立部署。
jar 文件需要特定的属性文件。我将这些属性文件放在 war 文件中的“src”文件夹下(即类层次结构的顶部)。
过去,所有内容都在同一个项目中并部署在一个 war 文件中。相关类可以访问属性文件。现在,当这些类部署在 jar 中时,它们无法看到位于 war 文件(即部署的 Web 应用程序)中的属性文件。
我在这里可能会缺少什么? 我如何加载 proeprties 文件的示例:
properties.load(getClass().getResourceAsStream("/appconfig.properties"));
谢谢您的宝贵时间。
Recently I separated the core functionality of my core servlets application into a jar file. This jar file is now deployed in tomcat's lib folder and the application(i.e. servlets, jsps, properties files..etc) is deployed independently as a war file.
The jar files needs specific properties files. I place these properties files right under the "src" folder(i.e. in the top of the classes hierarchy) in the war file.
In the past when everything was in the same project and deployed in one war file. The properties files were accessible by the related classes. Now when these classes are deployed in a jar, they can't see the properties files located in the war file (i.e. deployed web application).
What could I be missing here ?
An example how I load my proeprties files:
properties.load(getClass().getResourceAsStream("/appconfig.properties"));
Thank you for your time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不应该通过类自己的类加载器来获取它。由于该类现在由 Tomcat 管理,因此它只了解 Tomcat 的内部资源,而不了解特定于 Web 应用程序的资源。您应该通过当前线程的上下文类加载器获取它。该类加载器知道专用于当前 Web 应用程序的所有资源。
不管怎样,代表全局应用程序配置的此类属性文件的正确位置不会位于 WAR 文件内。您应该将属性文件放置在 Tomcat 和 WAR 文件外部的固定路径上,然后将该固定路径添加到 Tomcat 的
/conf/catalina.properties
shared.loader 属性中> 使其成为类路径的一部分。这允许您自由编辑配置文件,而无需重建/重新部署整个 WAR。请注意,您仍然需要使用上下文类加载器来加载它。You shouldn't get it by the class' own classloader. As the class is now managed by Tomcat, it only knows about Tomcat's internal resources, not about webapp-specific resources. You should get it by the context classloader of the current thread. That classloader knows about all resources dedicated to the current webapp.
Regardless of this, the proper location of such a properties file representing the global app configuration would not be inside the WAR file. You should rather place the properties file on a fixed path outside Tomcat and the WAR file and then add that fixed path to the
shared.loader
property of Tomcat's/conf/catalina.properties
so that it becomes part of the classpath. This allows you for freely editing the configuration file without the need to rebuild/redeploy the whole WAR. Note that you still need to use the context classloader to load it.是的,这不起作用,因为 tomcat lib 文件夹中的 jar 是由不同的类加载器加载的,而该类加载器对您的 web 应用程序的类一无所知。换句话说,tomcat lib 是 webapp 类加载器的父类加载器(为每个 webapp 创建一个)。
如果你想让它工作,你可以将属性文件放在外部位置,并通过绝对文件路径让 tomcat lib 内的 jar 知道它,或者将 jar 放回 webapp 的 lib 内。
以下是 Tomcat 站点的一些参考。
http://tomcat.apache.org/tomcat-7.0-doc /class-loader-howto.html
通常,环境变量用于标识 $CATALINA_HOME,然后将其作为相对文件路径的前缀,以避免对绝对路径进行硬编码。
Yes, that won't work because the jars in tomcat lib folder are loaded by a different classloader that knows nothing about your webapp's classes. In other words, tomcat lib is the parent class loader of the webapp classloader (which are created one per each webapp).
If you want to make it work, you can either place the properties file in an external location and make it known to the jar inside tomcat lib via absolute file path or place the jar back inside webapp's lib.
Here's some reference from the Tomcat site.
http://tomcat.apache.org/tomcat-7.0-doc/class-loader-howto.html
Typically, environment variables are used to identify $CATALINA_HOME which will then be prefixed to the relative file path in order to avoid hardcoding the absolute paths.