javaapache-commons-config

How do you use a CombinedConfiguration to override a default configuration?


When adding configurations to a CombinedConfiguration (from Apache Commons Configuration 2), how do you specify that one configuration overrides another?

For example, say I have a default configuration and a user configuration. If I want the user to override the default, how do I setup the the combined configuration?

XMLConfiguration defaultConfig = ...;
XMLConfiguration userConfig = ...;

CombinedConfiguration config = new CombinedConfiguration();
config.addConfiguration(defaultConfig);
config.addConfiguration(userConfig);

Solution

  • It depends on the which NodeCombiner passed to the constructor of the CombinedConfiguration class. For example if OverrideCombiner is used first item added to the configuration will take precedence over other nodes. If you call default constructor of CombinedConfiguration, UnionCombiner will be used.

    OverrideCombiner Docs

    MergeCombiner Docs

    UnionCombiner Docs