【问题标题】:How to reload apache commons configurations2 properties如何重新加载 apache commons 配置2 属性
【发布时间】:2017-02-10 02:36:09
【问题描述】:

谁能指导我如何重新加载 apache commons configuration2 属性。我无法在任何地方找到任何实现。 apache 文档有点太抽象了。这是我到目前为止所拥有的,但它不起作用。

    CombinedConfiguration cc = new CombinedConfiguration();

    Parameters params = new Parameters();
    File configFile = new File("config.properties");
    File emsFile = new File("anotherconfig.properties");

    ReloadingFileBasedConfigurationBuilder<FileBasedConfiguration> configBuilder =
        new ReloadingFileBasedConfigurationBuilder<FileBasedConfiguration>(PropertiesConfiguration.class)
        .configure(params.fileBased()
            .setFile(configFile));
    PeriodicReloadingTrigger reloadTrg = new PeriodicReloadingTrigger(configBuilder.getReloadingController(), null, 5, TimeUnit.SECONDS);
    reloadTrg.start();

    cc.addConfiguration(configBuilder.getConfiguration());

    FileBasedConfigurationBuilder<FileBasedConfiguration> emsBuilder =
            new FileBasedConfigurationBuilder<FileBasedConfiguration>(PropertiesConfiguration.class)
            .configure(params.properties()
                .setFile(emsFile));
    cc.addConfiguration(emsBuilder.getConfiguration());

    DataSource ds = EmsDataSource.getInstance().getDatasource(this);

     BasicConfigurationBuilder<DatabaseConfiguration> dbBuilder =
         new BasicConfigurationBuilder<DatabaseConfiguration>(DatabaseConfiguration.class);
     dbBuilder.configure(
         params.database()
             .setDataSource(ds)
             .setTable("EMS_CONFIG")
             .setKeyColumn("KEY")
             .setValueColumn("VALUE")
     );
    cc.addConfiguration(dbBuilder.getConfiguration());

【问题讨论】:

    标签: reload apache-commons properties-file apache-commons-config


    【解决方案1】:

    从构建器获得的配置不会自动更新。每次阅读时都需要从构建器获取配置。

    来自Automatic Reloading of Configuration Sources

    使用这种方法进行重新加载时要记住的重要一点是,只有将构建器用作访问配置数据的中心组件时,重新加载才起作用。从builder中获取的配置实例不会自动改变!因此,如果应用程序在启动时从构建器获取配置对象,然后在其整个生命周期中使用它,则外部配置文件上的更改将永远不可见。正确的做法是集中保留对构建器的引用,并在每次需要配置数据时从那里获取配置。

    【讨论】:

      【解决方案2】:

      使用以下代码:

      @Component
      public class ApplicationProperties {
          private PropertiesConfiguration configuration;
      
          @PostConstruct
          private void init() {
              try {
                  String filePath = PropertiesConstants.PROPERTIES_FILE_PATH;
                  System.out.println("Loading the properties file: " + filePath);
                  configuration = new PropertiesConfiguration(filePath);
      
                  //Create new FileChangedReloadingStrategy to reload the properties file based on the given time interval
                  FileChangedReloadingStrategy fileChangedReloadingStrategy = new FileChangedReloadingStrategy();
                 fileChangedReloadingStrategy.setRefreshDelay(PropertiesConstants.REFRESH_DELAY);
                  configuration.setReloadingStrategy(fileChangedReloadingStrategy);
              } catch (ConfigurationException e) {
                  e.printStackTrace();
              }
          }
      
          public String getProperty(String key) {
              return (String) configuration.getProperty(key);
          }
      
          public void setProperty(String key, Object value) {
              configuration.setProperty(key, value);
          }
      
          public void save() {
              try {
                  configuration.save();
              } catch (ConfigurationException e) {
                  e.printStackTrace();
              }
          }
      }
      

      【讨论】:

      • 这使用apache配置1而不是2
      猜你喜欢
      • 2023-04-09
      • 2011-08-31
      • 2013-06-24
      • 1970-01-01
      • 2016-06-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多