Debian Codegraph @

owner-site/site/docs/variables-expansion.md

raw symbols SCIP
1 ---
2 layout: docs
3 title: Variables expansion
4 prev_section: type-conversion
5 next_section: reload
6 permalink: /docs/variables-expansion/
7 ---
8
9 Sometimes it may be useful to expand properties values from other properties.
10
11 Let's have a look at this properties file:
12
13 ```properties
14 story=The ${animal} jumped over the ${target}
15 animal=quick ${color} fox
16 target=${target.attribute} dog
17 target.attribute=lazy
18 color=brown
19 ```
20
21 ...and the associated *mapping interface*:
22
23 ```java
24 public interface ConfigWithExpansion extends Config {
25 String story();
26 }
27 ```
28
29 The property `story` will expand to:
30
31 <blockquote>The quick brown fox jumped over the lazy dog</blockquote>
32
33 This also works with the annotations, but you need to specify every properties
34 on the methods:
35
36 ```java
37 public interface ConfigWithExpansion
38 extends Config {
39
40 @DefaultValue(
41 "The ${animal} jumped over the ${target}")
42 String story();
43
44 @DefaultValue("quick ${color} fox")
45 String animal();
46
47 @DefaultValue("${target.attribute} dog")
48 String target();
49
50 @Key("target.attribute")
51 @DefaultValue("lazy")
52 String targetAttribute();
53
54 @DefaultValue("brown")
55 String color();
56 }
57
58 ConfigWithExpansion conf = ConfigFactory
59 .create(ConfigWithExpansion.class);
60
61 String story = conf.story();
62 ```
63
64 Sometimes you may want expand System Properties or Environment Variables.
65 This can be done using *imports* (see
66 [Importing properties]({{ site.url }}/docs/importing-properties/) to learn
67 more):
68
69 ```java
70 public interface SystemPropertiesExample
71 extends Config {
72
73 @DefaultValue("Welcome: ${user.name}")
74 String welcomeString();
75
76 @DefaultValue("${TMPDIR}/tempFile.tmp")
77 File tempFile();
78
79 }
80
81 SystemPropertiesExample conf = ConfigFactory
82 .create(SystemPropertiesExample.class,
83 System.getProperties(),
84 System.getenv());
85
86 String welcome = conf.welcomeString();
87 File temp = conf.tempFile();
88 ```
89
90
91 Disabling variables expansion
92 -----------------------------
93
94 The variables expansion feature can be disabled if the user doesn't find it
95 convenient for some reason.
96 This can be done using the `@DisableFeature` annotation:
97
98 ```java
99 public interface Sample extends Config {
100 @DefaultValue("Earth")
101 String world();
102
103 @DisableFeature(VARIABLE_EXPANSION)
104 @DefaultValue("Hello ${world}.")
105
106 // will return the string "Hello ${world}."
107 String sayHello();
108 }
109
110 ```
111
112 The `@DisabledFeature` annotation can be applied on method level and/or on
113 interface level. When applied on interface level, the annotation will apply to
114 all methods defined in that interface.
115
116 Variable expansion for the @Key
117 ---------------------------------
118
119 Some time ago, I used to work for a company where - for security reason - it was required to deploy the exact same
120 artifact to production after being tried and verified in a testing environment.
121 The artifact was signed by a deployment tool to ensure that no changes were made during the passage from the
122 user acceptance test to the production environment. And the configuration was (correctly, in my opinion) considered part
123 of the deployment artifact.
124
125 The deployment artifact then, was containing all the runtime configurations, and the administrator or the application
126 itself was responsible to select the appropriate configuration to run with.
127 I really liked this approach, because was preventing having uncontrolled code running in prod/test enviroments. And
128 since configuration vastly impacts on how the application works, it must be versioned and under strict control as any
129 other source code.
130
131 Since version 1.0.6 it's possible to use variable expansion also in the `@Key` annotation, and this really adapts to
132 the above usage pattern, where we had all the configurations for different running enviroments in the same files,
133 and having the application or the administrator select the appropriate settings.
134
135 Suppose in your configuration you have defined three running environment: production
136 development and test. So you have a configuration XML looking like this:
137
138 ```xml
139 <servers>
140 <dev> <!-- development environment -->
141 <name>Development</name>
142 <hostname>devhost</hostname>
143 <port>6000</port>
144 <user>myuser1</user>
145 <password>mypass1</password>
146 </dev>
147 <uat> <!-- user acceptance test environment -->
148 <name>User Acceptance Test</name>
149 <hostname>uathost</hostname>
150 <port>60020</port>
151 <user>myuser2</user>
152 <password>mypass2</password>
153 </uat>
154 <prod> <!-- production environment -->
155 <name>Production</name>
156 <hostname>prod-host</hostname>
157 <port>600</port>
158 <user>prod-user</user>
159 <password>secret</password>
160 </prod>
161 </servers>
162 ```
163
164 Or - if you prefer - a properties file that is equivalent to the above:
165
166 ```properties
167 servers.dev.name=Development
168 servers.dev.hostname=devhost
169 servers.dev.port=6000
170 servers.dev.user=myuser1
171 servers.dev.password=mypass1
172
173 servers.uat.name=User Acceptance Test
174 servers.uat.hostname=uathost
175 servers.uat.port=60020
176 servers.uat.user=myuser2
177 servers.uat.password=mypass2
178
179 servers.prod.name=Production
180 servers.prod.hostname=prod-host
181 servers.prod.port=600
182 servers.prod.user=prod-user
183 servers.prod.password=secret
184 ```
185
186 You can define the configuration mapping file as:
187
188 ```java
189 @Sources("classpath:org/aeonbits/owner/variableexpansion/KeyExpansionExample.xml")
190 public interface ExpandsFromAnotherKey extends Config {
191
192 @DefaultValue("dev")
193 String env();
194
195 @Key("servers.${env}.name")
196 String name();
197
198 @Key("servers.${env}.hostname")
199 String hostname();
200
201 @Key("servers.${env}.port")
202 Integer port();
203
204 @Key("servers.${env}.user")
205 String user();
206
207 @Key("servers.${env}.password")
208 String password();
209 }
210 ```
211
212 Notice, on the above, that I defined the `env()` method to default with "dev" value. The subsequent `${env}` variables
213 specified in the `@Key` annotation would use "dev" as default value.
214
215 But now you can specify the `${env}` variable at runtime when creating the config object:
216
217 ```java
218
219 Map myVars = new HashMap();
220 myVars.put("env", "uat"); // here!
221
222 ExpandsFromAnotherKey cfg = ConfigFactory
223 .create(ExpandsFromAnotherKey.class, myVars); // here!
224
225 assertEquals("User Acceptance Test", cfg.name());
226 assertEquals("uathost", cfg.hostname());
227 assertEquals(new Integer(60020), cfg.port());
228 assertEquals("myuser2", cfg.user());
229 assertNull("mypass2", cfg.password());
230
231 ```
232
233 This way you can select `${env}` when using the `ConfigFactory` (or from the system properties
234 and/or the environment variables if you prefer) and have the above interface map
235 to the appropriate section.
236 In the above example I selected "uat" as value for the `${env}` variables, so the "user acceptance test" configuration
237 would be selected.