15

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

skaffman
  • 398,947
  • 96
  • 818
  • 769
shashuec
  • 684
  • 8
  • 20

3 Answers3

17

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:

Anand Nalya
  • 741
  • 8
  • 14
Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
  • I created a file "loader.properties" having two lines, include = a.properties and include = b.properties. It still includes the a.properties but not the b.properties. I used PropertyConfiguration to load the "loader.properties" file. Do you know why? – Sujoy Mar 17 '15 at 17:09
0

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.

PanHrabia
  • 174
  • 1
  • 4
0

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.