1. 程式人生 > >java不重啟服務動態載入properties檔案

java不重啟服務動態載入properties檔案

動態載入properties檔案內容,不需要重啟服務!

1 、Maven 工程,在resource下新建一個properties檔案

target/classes/config.properties

user=dufy
phoneNo=123456789

2、新建解析properties檔案的工具類

package com.dufy.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileInputStream;
import
java.io.IOException; import java.util.Collection; import java.util.Properties; /** * * * @author:dufyun * @version:1.0.0 * @date 2017/11/16 * @update:[日期YYYY-MM-DD] [更改人姓名][變更描述] */ public class PropertiesUtil { private static Logger log = LoggerFactory.getLogger(PropertiesUtil.class); private
static final String CONFIG_NAME = "config.properties"; private static Properties prop; private static Long lastModified = 0L; /** * 初始化載入配置檔案 */ private static void init() { prop = new Properties(); String filepath = PropertiesUtil.class.getClassLoader().getResource(CONFIG_NAME).getPath(); log.info(filepath); FileInputStream fis = null
; try { fis = new FileInputStream(filepath); prop.load(fis); } catch (IOException e) { log.error(CONFIG_NAME +"載入系統路徑資原始檔錯誤!"); e.printStackTrace(); }finally { try { if(fis != null){ fis.close(); } } catch (IOException e) { e.printStackTrace(); } } } /** * 判斷配置檔案是否改動 * @return returnValue :true:改動過 ,false:沒有改動過 */ private static boolean isPropertiesModified() { boolean returnValue = false; File file = new File(PropertiesUtil.class.getClassLoader().getResource(CONFIG_NAME).getPath()); if (file.lastModified() > lastModified) { log.info("修改CONFIG_NAME:{} 配置檔案",CONFIG_NAME); lastModified=file.lastModified(); returnValue = true; } return returnValue; } /** * 根據key獲取配置檔案中的值 * @param key key值 * @return 返回value */ private static String getPropertyValue(String key) { if (prop == null || isPropertiesModified()) { init(); } String value = prop.get(key).toString(); log.info("根據key獲取value值, key:{} ,value:{}",key,value); return value; } /** * 獲取配置檔案中所有的value值 * @return 所有配置的value值 */ private static String getPropertyAllValue() { if (prop == null || isPropertiesModified()) { init(); } Collection<Object> values = prop.values(); log.info("CONFIG_NAME :{},配置的所有的values:{}",CONFIG_NAME,values.toString()); return values.toString(); } /** * 驗證登入的手機號是否為測試手機號 * @param phoneNo 登入手機號 * @return true: 是測試賬戶, false:不是測試賬戶 */ public static boolean validateLoginNo(String phoneNo){ boolean flag = false; String allValue = getPropertyAllValue(); if(allValue.contains(phoneNo)){ flag = true; } return flag; } public static void main(String[] args) { while (true){ try { Thread.sleep(2000); System.out.println("validateLoginNo : " + validateLoginNo("123456789")); } catch (InterruptedException e) { e.printStackTrace(); } } } }

3、測試程式碼

  public static void main(String[] args) {
        while (true){
            try {
                Thread.sleep(2000);
                System.out.println("validateLoginNo :  " +  validateLoginNo("123456789"));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }

    }

啟動服務,然後控制檯列印
validateLoginNo : true;

然後在服務執行中,修改下面內容!

/target/classes/config.properties
phoneNo=987654321

validateLoginNo :  true
16:22:43.652 [main] INFO  c.s.m.l.c.c.util.PropertiesUtil - 修改CONFIG_NAME:config.properties 配置檔案
16:22:43.653 [main] INFO  c.s.m.l.c.c.util.PropertiesUtil - CONFIG_NAME :config.properties,配置的所有的values:[987654321]
validateLoginNo :  false


如果您覺得這篇博文對你有幫助,請點個贊,謝謝!


如果帥氣(美麗)、睿智(聰穎),和我一樣簡單善良的你看到本篇博文中存在問題,請指出,我虛心接受你讓我成長的批評,謝謝閱讀!
祝你今天開心愉快!

歡迎訪問我的csdn部落格,我們一同成長!

不管做什麼,只要堅持下去就會看到不一樣!在路上,不卑不亢!