【问题标题】:How to load multiple configuration files using apache common configuration(java)如何使用apache常用配置加载多个配置文件(java)
【发布时间】:2020-07-29 21:23:18
【问题描述】:

我有一个使用 apache 通用配置类加载的主 conf 文件。 我有一个要求,用户可以指定一个 conf 文件,并且这些文件中的值将覆盖主 conf 中的值。

请建议我如何在 apache 通用配置类或任何其他开源类中实现这一点。

提前致谢

【问题讨论】:

    标签: java xml apache-commons apache-commons-config


    【解决方案1】:

    我想你想要类似于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
    

    参考:

    【讨论】:

    • 我创建了一个包含两行的文件“loader.properties”,include = a.properties 和 include = b.properties。它仍然包括 a.properties,但不包括 b.properties。我使用 PropertyConfiguration 加载“loader.properties”文件。你知道为什么吗?
    【解决方案2】:

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

    然后以标准方式使用它来获取属性。

    【讨论】:

      【解决方案3】:

      PropertiesConfiguration 有一个方法copy(Configuration c) 将指定配置的内容复制到此配置中。如果指定的配置包含此配置中也存在的键,则此键的值将被新值替换。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-06-09
        • 1970-01-01
        • 2018-02-05
        • 1970-01-01
        • 2012-04-05
        • 2013-01-06
        相关资源
        最近更新 更多