| 1 | --- |
| 2 | layout: docs |
| 3 | title: Configuring the ConfigFactory |
| 4 | prev_section: disabling-features |
| 5 | next_section: xml-support |
| 6 | permalink: /docs/configuring/ |
| 7 | --- |
| 8 | |
| 9 | There isn't much to configure in OWNER since most of the things are done via annotations. |
| 10 | |
| 11 | Still somebody recently asked: I need to decide the sources locations at runtime, is it possible to pass them |
| 12 | explicitly? (Issue [#36] on GitHub). |
| 13 | |
| 14 | [#36]: https://github.com/lviggiano/owner/issues/36 |
| 15 | |
| 16 | The short answer was no, even though there was some options to work around this limit. |
| 17 | |
| 18 | But at the end, we agreed to add some configuration properties to configure the OWNER ConfigFactory itself. |
| 19 | |
| 20 | Let's take this as example: |
| 21 | |
| 22 | ```java |
| 23 | // notice ${mypath} here |
| 24 | @Sources("file:${mypath}/myconfig.properties"); |
| 25 | interface MyConfig extends Config { ... } |
| 26 | |
| 27 | MyConfig cfg = ConfigFactory.create(MyConfig.class); |
| 28 | ``` |
| 29 | |
| 30 | The variable ${mypath} would be expanded from the System properties or from the environment properties, if defined |
| 31 | there. But this isn't convenient enough: environment properties are read only, and sometimes it is not convenient to |
| 32 | change system properties, especially for applications deployed in shared JVMs. |
| 33 | |
| 34 | So OWNER does have some context properties associated to the ConfigFactory itself: |
| 35 | |
| 36 | ```java |
| 37 | // notice ${mypath} here |
| 38 | @Sources("file:${mypath}/myconfig.properties"); |
| 39 | interface MyConfig extends Config { ... } |
| 40 | |
| 41 | // notice ${mypath} here |
| 42 | ConfigFactory.setProperty("mypath", "/foo/bar/baz"); |
| 43 | MyConfig cfg = ConfigFactory.create(MyConfig.class); |
| 44 | ``` |
| 45 | |
| 46 | When the `create()` method is executed the "file:${mypath}/myconfig.properties" specified by `@Sources` will be expanded |
| 47 | to "file:/foo/bar/baz/myconfig.properties". |
| 48 | |
| 49 | Notice that, if you later change the value in the ConfigFactory and try to reload the Config object, this will not be |
| 50 | affected, since the config object works on a snapshot of the those properties generated when the `create()` method is |
| 51 | invoked. |
| 52 | |
| 53 | Nothing prevents you to do something like: |
| 54 | |
| 55 | |
| 56 | ```java |
| 57 | // notice ${myurl} here |
| 58 | @Sources("${myurl}"); |
| 59 | interface MyConfig extends Config { ... } |
| 60 | |
| 61 | // notice ${myurl} here |
| 62 | ConfigFactory.setProperty( |
| 63 | "myurl", "http://somewhere.com/conf.properties"); |
| 64 | MyConfig cfg = ConfigFactory.create(MyConfig.class); |
| 65 | ``` |
| 66 | |
| 67 | You are completely free to define the sources of your configuration at runtime. |