WebSphere Liberty:从库 jar 文件访问属性文件

WebSphere Liberty: Access properties file from library jar file

我 运行 使用 WebSphere Liberty 17.0.0.4。部署了位于 {wlp_install_dir}/lib 目录下的 Web 应用程序和自定义身份验证模块。该 jar 文件在 server.xml 文件中标记为 library。这是它在 server.xml

中的样子
<library id="CustomLoginModuleLib">
    <fileset dir="${wlp.lib.dir}" includes="custom_auth.jar"/>
</library>

现在的问题是,我想将位于 custom_auth.jar 文件内的 .properties 文件用于 Web 应用程序。

已尝试通过以下代码片段访问:

this.getClass().getResourceAsStream("location/of/package/file.properties");

ClassLoader.getSystemResourceAsStream("location/of/package/file.properties");

但是,两者都不起作用。

知道我们如何访问位于库 jar 文件中的属性文件。

请在 dwAnswers 上查看我对同一问题的回复: https://developer.ibm.com/answers/questions/444708/how-to-access-properties-file-located-in-library-j.html

从那里总结答案:

(1) 我绝不会建议将用户提供的 JAR 文件放在 {wlp_install_dir}/lib 目录中 - 该目录仅适用于 IBM 提供的 JAR 文件。相反,我建议将 custom_auth.jar 放在服务器目录或共享目录中。

(2) 您需要将共享库与您的应用程序相关联,如下所示:

 <application location ="{appName}.war"> <!-- or {appName}.ear -->
    <classloader commonLibraryRef="CustomLoginModuleLib" />
 </application>

根据您的需要,您可以使用 commonLibraryRef(如图所示)或 privateLibraryRef。可以在此处找到有关共享库的更多信息:https://www.ibm.com/support/knowledgecenter/SSD28V_9.0.0/com.ibm.websphere.wlp.core.doc/ae/cwlp_sharedlibrary.html

(3) 至于在 Java 代码中加载文件,您的第一行将起作用 - 假设这是指应用程序中 class 的一个实例。我还假设您传递给 getResourceAsStream 方法的路径与库 JAR 中文件的路径相同。

安迪,希望这对你有所帮助