开发者社区 问答 正文

jfinal PropKit 使用配置文件如何不缓存(能够即时生效):报错

@jFinal 

你好向您请教一个问题,我单独使用PropKit读取一个配置问题,然后启动系统,配置文件里面有2种登录方式,需要管理员可配置。即设置为a则登录的时候使用a的方式验证,设置为b则使用b的方式验证。

现在问题如下:

    系统启动后,修改配置文件不会立即生效,需要重启。调用PropKit.clear()也没有用。请问有什么好的处理方式吗?

-**--*-

展开
收起
kun坤 2020-06-06 17:05:44 634 分享 版权
1 条回答
写回答
取消 提交回答
  • 已解决下面是方案:

    使用commons-configuration1.10插件支持properties热加载。注意需要同时加入commons-lang2.6和commons-logging-1.1.1 .

    改写了下jfinal的Prop

    /**
    * 重载
    * @param fileName
    * @param encoding
    */
    public Prop(String fileName, String encoding ,boolean isReplace) {
    InputStream inputStream = null;
    try {

    if (fileName == null)
    throw new IllegalArgumentException("Properties file not found in classpath: " + fileName);
    PropertiesConfiguration cfg = new PropertiesConfiguration(fileName);
    cfg.setReloadingStrategy(new FileChangedReloadingStrategy());
    cfg.setEncoding(encoding);
    cfg.load();
    Iterator<String> iterator = cfg.getKeys();
    properties = new Properties();
    while(iterator.hasNext()){
    String key = iterator.next();
    properties.setProperty(key,cfg.getString(key));
    }
    } catch (Exception e) {
    throw new RuntimeException("Other error.", e);

    finally {
    if (inputStream != null) try {inputStream.close();} catch (IOException e) {e.printStackTrace();}
    }
    }

    2020-06-06 17:05:48
    赞同 展开评论