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);  

  


 

转载于:https://www.cnblogs.com/jdy1022/p/11076346.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值