首页 > 解决方案 > wildfly:从类路径目录中读取属性文件

问题描述

我正在尝试从我的 claspath 文件夹中的属性文件中读取部署特定信息。War 文件通过域模式部署在 Wildfly 13 上。

/opt/wildfly/domain/servers/APPS1/tmp/vfs/temp/tempe22b1a59c629344/content-35e1234535af2e86/WEB-INF/classes/bis/com/test/config/application.properties

当我尝试使用以下方法获取路径时

System.getProperty(jboss.server.temp.dir) 

我只得到 /opt/wildfly/domain/servers/APPS1/tmp

我正在从此类 bis.com.test.module.app 运行此代码

bis|com|test|module|app.class

bis|com|test|config|application.properties

我努力了

private static final String PATH = "/WEB-INF/classes/bis/com/test/config/application.properties";

Properties props = new Properties();
InputStream in = getClass().getResourceAsStream("/application.properties");
props.load(in);
String JDBC = props.getProperty("jdbc.connection");

Properties props = new Properties();
InputStream in = App.class().getResourceAsStream("/application.properties");
props.load(in);
String JDBC = props.getProperty("jdbc.connection");

Properties props = new Properties();
InputStream in = getClass().getResourceAsStream(PATH);
props.load(in);
String JDBC = props.getProperty("jdbc.connection");

URL url = FacesContext.getCurrentInstance().getExternalContext().getResource(PATH);
Properties app = new Properties();
           app.load(url.openStream());
String JDBC = app.getProperty("jdbc.connection");

但是我得到所有的空指针异常......我如何绕过/访问/检索临时文件夹并进入 WEB-INF 文件夹?由于当前的设计逻辑,我无法将应用程序属性移动到其他地方。我只能在这里访问属性文件。如何?

标签: javaurljbosswildfly

解决方案


经过一番挖掘,我发现您application.properties位于一个包下,因此应按以下方式访问它:

ClassLoader loader = getClass().getClassLoader();
inputStream = loader.getResourceAsStream("/bis/com/test/config/application.properties");

推荐阅读