在应用程序处于 运行 时更改属性文件

Changing the properties file while the application is running

我在属性文件中有一个属性。说 'x'。 在我的 Java class 中,我在循环中使用了这个 x。所以第一次执行循环时,它从属性文件加载,从第二次开始,它从内存中获取值,而不是每次都加载 props 文件。现在,如果我想更改属性文件中 x 的值,是否可以在不重新启动应用程序的情况下加载该值?如果是,如何?

此外,在 .net 中是否有任何 Java 等同于 Session_OnStart?我听说 .net 中的 Session_OnStart 可用于此目的

您可以在每次请求变量时加载和解析属性。

class RefreshingProperties extends Properties {

  private final File file;

  public RefreshingProperties (File file) throws IOException { 
    this.file = file;
    refresh ();
  }

  private void refresh () throws IOException {
    load (new FileInputStream (file));
  }

  @Override
  public String getProperty (String name) {
    try { refresh (); }
    catch (IOException e) {}
    return super.get (name);
  }
}

您可以将其调整为仅在特定时间段到期时重新加载