Fork me on GitHub

Example Commons Configuration

An example how secured-properties can be used with Commons Configuration.


The following steps are required to initialize Commons Configuration:

  • First get the decrypted password.
  • Store the decrypted password into SystemProperties under the same key.
  • create a SystemConfiguration
  • create a PropertiesConfiguration
  • create a CompositeConfiguration with the SystemConfiguration and the PropertiesConfiguration
  • FINSHED: use the CompositeConfiguration as usual (e.g. per dependency injection).

In this example, the SystemProperties is only a working example to provide the decrypted password. Better would be to use a custom in-memory implementations of the Configuration.

The Property File

The example property-file “”:

myTitle = My Test

The Java Code

The java code example:

        // initialization
        File secretKey = new File("src/test/data/secretFileExample.key");
        File propertiesFile = new File("src/test/data/");

        // initialization - get decrypted value
        SecuredPropertiesConfig config = new SecuredPropertiesConfig().withSecretFile(secretKey).initDefault();
        // initialization - auto encrypt values in the property files:
                config, propertiesFile, "mySecretPassword");
        // initialization - get decrypted value
        String myPassword = SecuredProperties.getSecretValue(
                config, propertiesFile, "mySecretPassword");
        // initialization - store plane-text-PW into System-Properties
        System.setProperty("mySecretPassword", myPassword);

        // initialization - create CompositeConfiguration which first reads SystemProperties and then the PropertiesFile.
        SystemConfiguration systemConfiguration = new SystemConfiguration();

        PropertiesConfiguration propertiesConfiguration =
            new FileBasedConfigurationBuilder<>(PropertiesConfiguration.class)
                .configure(new Parameters().properties().setFile(propertiesFile))

        CompositeConfiguration commonConfig = new CompositeConfiguration();

        // somewhere in your application (e.g. with dependency injection of commonConfig):
        assertThat(commonConfig.getString("myTitle"), is("My Test")); // some value from PropertyFile
        assertThat(commonConfig.getString("mySecretPassword"), is("test")); // decrypted Password from SystemProperties.

The complete code is in