簡體   English   中英

Web應用程序中的屬性文件

[英]properties file in web app

首先,有很多解決方案,我已經閱讀了很多解決方案。 但由於某種原因,我沒有讓它發揮作用。

我正在嘗試將我的配置數據外包給我的webapp,以便我可以在部署之后進行配置。

那是我的物業服務:

   public class PropertiesService {

 Properties properties;
     public PropertiesService() {
      try {
       properties = new Properties();
       ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
       InputStream stream = classLoader.getResourceAsStream("META-INF/config.properties");
       properties.load(stream);
      } catch (Exception e) {
       e.printStackTrace();
      }
     }

     public String getHost(){
      return properties.getProperty("server_host");
     }

     public String getServerName(){
      return properties.getProperty("server_naming");
     }
    }

調試后我注意到變量流仍為空! 但我不知道為什么 - .-

需要幫忙 :-)

這里的錯誤日志:

java.lang.NullPointerException
 at java.util.Properties$LineReader.readLine(Properties.java:418)
 at java.util.Properties.load0(Properties.java:337)
 at java.util.Properties.load(Properties.java:325)

更新

我現在做以下事情:

properties.load(this.getClass().getResourceStream("/config/config.properties"));

我仍然得到一個nullPointerException

取出META-INF ,將它放在src/config直接配置源代碼中的包,在構建時它將轉到/WEB-INF/classes/config/config.properties

this.getClass().getResourceAsStream("/config/config.properties");

更新 在此輸入圖像描述

然后我在同一項目中創建了一個具有方法的Service類。

public static InputStream getConfigAsInputStream(){
    return Service.class.getResourceAsStream("/config/config.properties");
}

這工作.. !!

比較你的

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM