java 更改properties 檔案 不需要重啟web服務讀取最新修改的值
阿新 • • 發佈:2019-02-05
在專案開發中 有寫配置是寫在 properties 檔案中的 ,但有的時候需要更改值,但是發現需要重啟服務才能生效
後來發現 這種載入方法會將config.properties檔案載入到記憶體中,在下次需要讀取時直接從記憶體中獲取檔案資訊,而不是再次讀取!/** * 初始化配置檔案 */ public void init(){ try{ InputStream is = Config.class.getResourceAsStream("/res/config.properties"); properties = new Properties(); properties.load(is); }catch (Exception e){ throw new RuntimeException("Failed to get properties!"); } }
所以需要 改變一下檔案的輸入流獲取方式
String path = Config.class.getClassLoader().getResource("/res/config.properties").getPath();
InputStream is = new FileInputStream(path);
properties.load(is);