简体   繁体   English

在tomcat的lib中的jar在我的webapp中看不到属性文件

[英]A jar in tomcat's lib can't see a properties file in my webapp

Recently I separated the core functionality of my core servlets application into a jar file.最近,我将核心 servlets 应用程序的核心功能分离到 jar 文件中。 This jar file is now deployed in tomcat's lib folder and the application(ie servlets, jsps, properties files..etc) is deployed independently as a war file.这个 jar 文件现在部署在 tomcat 的 lib 文件夹中,并且应用程序(即 servlets、jsps、属性文件..等)作为 war 文件独立部署。

The jar files needs specific properties files. jar 文件需要特定的属性文件。 I place these properties files right under the "src" folder(ie in the top of the classes hierarchy) in the war file.我将这些属性文件放在war文件中的“src”文件夹下(即在类层次结构的顶部)。

In the past when everything was in the same project and deployed in one war file.过去,当一切都在同一个项目中并部署在一个 war 文件中时。 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 (ie deployed web application).现在,当这些类部署在 jar 中时,它们无法看到位于 war 文件中的属性文件(即部署的 web 应用程序)。

What could I be missing here?我在这里能错过什么? An example how I load my proeprties files:我如何加载我的 proeprties 文件的示例:

properties.load(getClass().getResourceAsStream("/appconfig.properties"));

Thank you for your time.感谢您的时间。

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.由于 class 现在由 Tomcat 管理,它只知道 Tomcat 的内部资源,而不知道 webapp 特定的资源。 You should get it by the context classloader of the current thread.您应该通过当前线程的上下文类加载器来获取它。 That classloader knows about all resources dedicated to the current webapp.该类加载器知道专用于当前 web 应用程序的所有资源。

properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("appconfig.properties"));

Regardless of this, the proper location of such a properties file representing the global app configuration would not be inside the WAR file.无论如何,表示全局应用程序配置的此类属性文件的正确位置不会在 WAR 文件中。 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.您应该将属性文件放在 Tomcat 和 WAR 文件之外的固定路径上,然后将该固定路径添加到 Tomcat 的/conf/catalina.propertiesshared.loader属性中,使其成为类路径的一部分。 This allows you for freely editing the configuration file without the need to rebuild/redeploy the whole WAR.这允许您自由编辑配置文件,而无需重建/重新部署整个 WAR。 Note that you still need to use the context classloader to load it.请注意,您仍然需要使用上下文类加载器来加载它。

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.是的,这是行不通的,因为 tomcat lib 文件夹中的 jars 是由不同的类加载器加载的,该类加载器对您的 webapp 的类一无所知。 In other words, tomcat lib is the parent class loader of the webapp classloader (which are created one per each webapp).换句话说,tomcat lib 是 webapp 类加载器的父 class 加载器(每个 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.如果你想让它工作,你可以将属性文件放在外部位置,并通过绝对文件路径将 tomcat lib 中的 jar 知道,或者将 jar 放回 webapp's 中。

Here's some reference from the Tomcat site.这是来自 Tomcat 站点的一些参考资料。

http://tomcat.apache.org/tomcat-7.0-doc/class-loader-howto.html 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.通常,环境变量用于标识 $CATALINA_HOME,然后将其作为相对文件路径的前缀,以避免对绝对路径进行硬编码。

String catalinaHome = System.getProperty("CATALINA_HOME");
properties.load(new FileInputStream(new File(catalinaHome + "/path/to/appconfig.properties")));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM