| 1 | --- |
| 2 | layout: docs |
| 3 | title: Loading strategies |
| 4 | prev_section: usage |
| 5 | next_section: importing-properties |
| 6 | permalink: /docs/loading-strategies/ |
| 7 | --- |
| 8 | |
| 9 | The properties file for a *mapping interface* is automatically resolved by OWNER |
| 10 | API by matching the class name and the file name (appending `.properties` of |
| 11 | course). |
| 12 | |
| 13 | But this logic can be tailored to your needs using some additional annotations: |
| 14 | |
| 15 | ```java |
| 16 | @Sources({ "file:~/.myapp.config", |
| 17 | "file:/etc/myapp.config", |
| 18 | "classpath:foo/bar/baz.properties" }) |
| 19 | public interface ServerConfig extends Config { |
| 20 | @Key("server.http.port") |
| 21 | int port(); |
| 22 | |
| 23 | @Key("server.host.name") |
| 24 | String hostname(); |
| 25 | |
| 26 | @Key("server.max.threads"); |
| 27 | @DefaultValue("42") |
| 28 | int maxThreads(); |
| 29 | } |
| 30 | ``` |
| 31 | |
| 32 | [properties]: http://docs.oracle.com/javase/7/docs/api/java/util/Properties.html |
| 33 | |
| 34 | |
| 35 | In the above example, OWNER will try to load the properties from several `@Sources`: |
| 36 | |
| 37 | 1. First, it will try to load the properties file from user's home directory ~/.myapp.config, if this is found, this |
| 38 | file alone will be used. |
| 39 | 2. If the previous attempt fails, then it will try to load the properties file from /etc/myapp.config, and if this is |
| 40 | found, this one will be used. |
| 41 | 3. As last resort, it will try to load the properties from the classpath loading the resource identified by the |
| 42 | path foo/bar/baz.properties. |
| 43 | 4. If none of the previous URL resources is found, then the Java interface will not be associated to any file, and only |
| 44 | `@DefaultValue` will be used where specified. Where properties don't have a default value, `null` will be returned |
| 45 | (as it happens for [`java.util.Properties`][properties]). |
| 46 | |
| 47 | In the above case, the properties values will be loaded from only one file: the first that is found. |
| 48 | *Only the first available properties file will be loaded, others will be ignored*. |
| 49 | |
| 50 | This load logic, is identified as *"FIRST"*, since only the first file found will be considered, and it is the default |
| 51 | logic adopted when the `@Source` annotation is specified with multiple URLs. |
| 52 | You can also specify this load policy explicitly using `@LoadPolicy(LoadType.FIRST)` on the interface declaration. |
| 53 | |
| 54 | But what if you want to have some *overriding* between properties? This is definitely possible: you can do it with |
| 55 | the annotation `@LoadPolicy(LoadType.MERGE)`: |
| 56 | |
| 57 | ```java |
| 58 | @LoadPolicy(LoadType.MERGE) |
| 59 | @Sources({ "file:~/.myapp.config", |
| 60 | "file:/etc/myapp.config", |
| 61 | "classpath:foo/bar/baz.properties", |
| 62 | "system:properties", |
| 63 | "system:env" }) |
| 64 | public interface ServerConfig extends Config { |
| 65 | ... |
| 66 | } |
| 67 | ``` |
| 68 | |
| 69 | In this case, for *every property* all the specified URLs will be queries, and the first resource defining the property |
| 70 | will prevail. |
| 71 | More in detail, this is what will happen: |
| 72 | |
| 73 | 1. First, it will try to load the given property from ~/.myapp.config; |
| 74 | if the given property is found the associated value will be returned. |
| 75 | 2. Then it will try to load the given property from /etc/myapp.config; |
| 76 | if the property is found the value associated will be returned. |
| 77 | 3. Then it will try to load the given property from the classpath from the resource identified |
| 78 | by the path foo/bar/baz.properties; if the property is found, the associated value is returned. |
| 79 | 4. Otherwise, it will try to load the given property from the <a href="https://docs.oracle.com/javase/tutorial/essential/environment/sysprop.html">Java system properties</a>; |
| 80 | if such property is defined, the associated value is returned. |
| 81 | 5. As last resort, it will try to load the given property from the <a href="https://docs.oracle.com/javase/tutorial/essential/environment/env.html">operating system's environment variables</a>; |
| 82 | if an environment variable with the same name is found, its value will be returned. |
| 83 | 6. If the given property is not found of any of the above cases, it will be returned the value specified by the |
| 84 | `@DefaultValue` if specified, otherwise null will be returned. |
| 85 | |
| 86 | So basically we produce a merge between the properties files where the first property files overrides latter ones. |
| 87 | |
| 88 | The `@Sources` annotation considers system properties and/or environment variables with the syntax |
| 89 | `file:${user.home}/.myapp.config` (this gets resolved by 'user.home' system property) or `file:${HOME}/.myapp.config` |
| 90 | (this gets resolved by the$HOME environment variable). The `~` used in the previous example is another example of |
| 91 | variable expansion, and it is equivalent to `${user.home}`. |