java 更改properties 文件 不需要重启web服务读取最新修改的值

1.在项目开发中 有写配置是写在 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!");
        }
    }

后来发现 这种加载方法会将config.properties文件加载到内存中,在下次需要读取时直接从内存中获取文件信息,而不是再次读取!所以需要 改变一下文件的输入流获取方式 

     String path = Config.class.getClassLoader().getResource("/res/config.properties").getPath();  
	InputStream is = new FileInputStream(path);  
	properties.load(is);  

  


 

猜你喜欢

转载自www.cnblogs.com/jdy1022/p/11076346.html