--- layout: docs title: Variables expansion prev_section: type-conversion next_section: reload permalink: /docs/variables-expansion/ --- Sometimes it may be useful to expand properties values from other properties. Let's have a look at this properties file: ```properties story=The ${animal} jumped over the ${target} animal=quick ${color} fox target=${target.attribute} dog target.attribute=lazy color=brown ``` ...and the associated *mapping interface*: ```java public interface ConfigWithExpansion extends Config { String story(); } ``` The property `story` will expand to:
The quick brown fox jumped over the lazy dogThis also works with the annotations, but you need to specify every properties on the methods: ```java public interface ConfigWithExpansion extends Config { @DefaultValue( "The ${animal} jumped over the ${target}") String story(); @DefaultValue("quick ${color} fox") String animal(); @DefaultValue("${target.attribute} dog") String target(); @Key("target.attribute") @DefaultValue("lazy") String targetAttribute(); @DefaultValue("brown") String color(); } ConfigWithExpansion conf = ConfigFactory .create(ConfigWithExpansion.class); String story = conf.story(); ``` Sometimes you may want expand System Properties or Environment Variables. This can be done using *imports* (see [Importing properties]({{ site.url }}/docs/importing-properties/) to learn more): ```java public interface SystemPropertiesExample extends Config { @DefaultValue("Welcome: ${user.name}") String welcomeString(); @DefaultValue("${TMPDIR}/tempFile.tmp") File tempFile(); } SystemPropertiesExample conf = ConfigFactory .create(SystemPropertiesExample.class, System.getProperties(), System.getenv()); String welcome = conf.welcomeString(); File temp = conf.tempFile(); ``` Disabling variables expansion ----------------------------- The variables expansion feature can be disabled if the user doesn't find it convenient for some reason. This can be done using the `@DisableFeature` annotation: ```java public interface Sample extends Config { @DefaultValue("Earth") String world(); @DisableFeature(VARIABLE_EXPANSION) @DefaultValue("Hello ${world}.") // will return the string "Hello ${world}." String sayHello(); } ``` The `@DisabledFeature` annotation can be applied on method level and/or on interface level. When applied on interface level, the annotation will apply to all methods defined in that interface. Variable expansion for the @Key --------------------------------- Some time ago, I used to work for a company where - for security reason - it was required to deploy the exact same artifact to production after being tried and verified in a testing environment. The artifact was signed by a deployment tool to ensure that no changes were made during the passage from the user acceptance test to the production environment. And the configuration was (correctly, in my opinion) considered part of the deployment artifact. The deployment artifact then, was containing all the runtime configurations, and the administrator or the application itself was responsible to select the appropriate configuration to run with. I really liked this approach, because was preventing having uncontrolled code running in prod/test enviroments. And since configuration vastly impacts on how the application works, it must be versioned and under strict control as any other source code. Since version 1.0.6 it's possible to use variable expansion also in the `@Key` annotation, and this really adapts to the above usage pattern, where we had all the configurations for different running enviroments in the same files, and having the application or the administrator select the appropriate settings. Suppose in your configuration you have defined three running environment: production development and test. So you have a configuration XML looking like this: ```xml