Apache Commons logo Commons Configuration

Composite Configuration Details

There are many use cases when you want to collect the properties of several configuration sources and access them like a single configuration object. One way to do that is using the CompositeConfiguration class.

A CompositeConfiguration object contains a list of other configuration objects. When properties are accessed from a composite configuration the object takes the passed in property key and iterates over the list of the contained configurations. As soon as a value is found for the key it is returned. This means that a CompositeConfiguration implements a kind of override semantics, i.e. the properties of configurations that were added first hide the property values of configurations added later.

We will discuss how you can establish a "default" choice for your Composite Configuration as well as save changes made to your Composite Configuration.

Setting Up Defaults

Defaults are very simple. You can just add them as your last configuration object:

Parameters params = new Parameters();
FileBasedConfigurationBuilder<Configuration> builderDefaults =
    new FileBasedConfigurationBuilder<Configuration>(PropertiesConfiguration.class)
FileBasedConfigurationBuilder<Configuration> builderOther =
    new FileBasedConfigurationBuilder<Configuration>(PropertiesConfiguration.class)

CompositeConfiguration cc = new CompositeConfiguration();

Saving Changes

If you have a non static Configuration where you want to save changes made to a configuration, and you are using a CompositeConfiguration, then you will need to pass into the constructor of the CompositeConfiguration what Configuration to save the changes via.

Parameters params = new Parameters();
FileBasedConfigurationBuilder<Configuration> builder =
    new FileBasedConfigurationBuilder<Configuration>(PropertiesConfiguration.class, null, true)

Configuration cc = new CompositeConfiguration(builder.getConfiguration());
cc.setProperty("newProperty","new value");


Alternatively, you can just request the in-memory configuration that stores the changes. The following example fragment copies all properties from the in-memory configuration into a PropertiesConfiguration, which can be saved later:

Configuration changes = myCompositeConfiguration.getInMemoryConfiguration();
FileBasedConfigurationBuilder<PropertiesConfiguration> builder =
    new FileBasedConfigurationBuilder<PropertiesConfiguration>(PropertiesConfiguration.class);
PropertiesConfiguration config = builder.getConfiguration();
for (Iterator<String> i = changes.getKeys(); i.hasNext()){
    String key = i.next();
    Object value = changes.get(key);