[英]wildfly: reading properties file from classpath directory
我正在嘗試從我的 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 文件夾? 由於當前的設計邏輯,我無法將應用程序屬性移到別處。 我只能在這里訪問屬性文件。 如何?
經過一番挖掘,我發現您的application.properties
位於一個包下,因此應該按如下方式訪問它:
ClassLoader loader = getClass().getClassLoader();
inputStream = loader.getResourceAsStream("/bis/com/test/config/application.properties");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.