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
A 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 DefaultsDefaults are very simple. You can just add them as your last configuration object, either through the ConfigurationFactory or manually: Configuration defaults = new PropertiesConfiguration(fileToDefaults); Configuration otherProperties = new PropertiesConfiguration(fileToOtherProperties); CompositeConfiguration cc = new CompositeConfiguration(); cc.addConfiguration(otherProperties); cc.addConfiguration(fileToDefaults); Saving ChangesIf 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. PropertiesConfiguration saveConfiguration = new PropertiesConfiguration(fileToSaveChangesIn); Configuration cc = new CompositeConfiguration(saveConfiguration); cc.setProperty("newProperty","new value"); saveConfiguration.save();
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 Configuration changes = myCompositeConfiguration.getInMemoryConfiguration(); DatabaseConfiguration config = new DatabaseConfiguration(datasource, "configuration", "key", "value"); for (Iterator<String> i = changes.getKeys(); i.hasNext()){ String key = i.next(); Object value = changes.get(key); config.setProperty(key,value); } |