0

任何人都可以指導我如何執行apache commons configuration2屬性的重新加載。我無法在任何地方找到這個實現。 apache文檔太抽象了。這是我迄今爲止的,但它不起作用。如何重新加載apache commons configurations2屬性

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

回答

0

從構建器獲取的配置不會自動更新。每次閱讀時都需要從構建器獲取配置。

Automatic Reloading of Configuration Sources

一個重要點使用此方法時重新加載要記住的是,如果構建器被用作中央組件來訪問配置數據重新加載僅爲功能性。從構建器獲取的配置實例不會自動更改!因此,如果應用程序在啓動時從構建器獲取配置對象,然後在其整個生命週期中使用它,外部配置文件上的更改將永遠不可見。正確的方法是集中引用構建器,並在每次需要配置數據時從那裏獲取配置。

0

使用下面的代碼:

@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(); 
     } 
    } 
} 
相關問題