How to load multiple configuration files using apache common configuration(java)

后端 未结 3 1392
耶瑟儿~
耶瑟儿~ 2021-02-07 11:52

I have a main conf file which I load using apache common configuration class. I have a requirement where user can specify a conf file and values in those file will override the

相关标签:
3条回答
  • 2021-02-07 12:10

    PropertiesConfiguration has a method copy(Configuration c) which copies the content of the specified configuration into this configuration. If the specified configuration contains a key that is also present in this configuration, the value of this key will be replaced by the new value.

    0 讨论(0)
  • 2021-02-07 12:13

    I think you want something similar to the mechanism described here:

    CompositeConfiguration config = new CompositeConfiguration();
    config.addConfiguration(new PropertiesConfiguration("user.properties"));
    config.addConfiguration(
        new PropertiesConfiguration("application.properties"));
    // user preferences have precedence over application preferences
    

    Reference:

    • CompositeConfiguration
    0 讨论(0)
  • 2021-02-07 12:29

    With cfg4j:

    // Specify which files to load. Configuration from both files will be merged.
    ConfigFilesProvider configFilesProvider = () -> Arrays.asList(new File("application.properties"), new File("otherConfig.properties"));
    
    // Use local files as configuration store
    ConfigurationSource source = new FilesConfigurationSource(configFilesProvider);
    

    Then use it in a standard way to get properties out.

    0 讨论(0)
提交回复
热议问题