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

≯℡__Kan透↙ 提交于 2019-12-03 17:13:14

问题


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 values in main conf.

Please suggest me how we can do that in apache common configuration class or any other open source class to achieve this.

Thanks in advance


回答1:


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



回答2:


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.



来源:https://stackoverflow.com/questions/5115324/how-to-load-multiple-configuration-files-using-apache-common-configurationjava

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!