| 1 | --- |
| 2 | layout: docs |
| 3 | title: Importing properties |
| 4 | prev_section: loading-strategies |
| 5 | next_section: parametrized-properties |
| 6 | permalink: /docs/importing-properties/ |
| 7 | --- |
| 8 | You can use another mechanism to load your properties into a *mapping interface*. |
| 9 | And this mechanism is to specify a Properties object programmatically when |
| 10 | calling `ConfigFactory.create()`: |
| 11 | |
| 12 | ```java |
| 13 | public interface ImportConfig extends Config { |
| 14 | |
| 15 | @DefaultValue("apple") |
| 16 | String foo(); |
| 17 | |
| 18 | @DefaultValue("pear") |
| 19 | String bar(); |
| 20 | |
| 21 | @DefaultValue("orange") |
| 22 | String baz(); |
| 23 | |
| 24 | } |
| 25 | |
| 26 | // then... |
| 27 | |
| 28 | Properties props = new Properties(); |
| 29 | props.setProperty("foo", "pineapple"); |
| 30 | props.setProperty("bar", "lime"); |
| 31 | |
| 32 | ImportConfig cfg = ConfigFactory |
| 33 | .create(ImportConfig.class, props); // props imported! |
| 34 | |
| 35 | assertEquals("pineapple", cfg.foo()); |
| 36 | assertEquals("lime", cfg.bar()); |
| 37 | assertEquals("orange", cfg.baz()); |
| 38 | ``` |
| 39 | |
| 40 | <div class="note warning"> |
| 41 | <h5>Null keys and Null values are invalid.</h5> |
| 42 | |
| 43 | <p> |
| 44 | A <code>Properties</code> or <code>Map</code> object accepts <code>null</code> key or |
| 45 | <code>null</code> values, but that is obviously an error, |
| 46 | so starting from version 1.0.10, an <code>IllegalArgumentException</code> is thrown. |
| 47 | </p> |
| 48 | |
| 49 | <p> |
| 50 | The exception message also contains further information about the offending key, |
| 51 | if applicable. |
| 52 | </p> |
| 53 | </div> |
| 54 | |
| 55 | You can specify multiple properties to import on the same line: |
| 56 | |
| 57 | ```java |
| 58 | ImportConfig cfg = ConfigFactory |
| 59 | .create(ImportConfig.class, props1, props2, ...); |
| 60 | ``` |
| 61 | |
| 62 | If there are prop1 and prop2 defining two different values for the same property |
| 63 | key, the one specified first will prevail: |
| 64 | |
| 65 | ```java |
| 66 | Properties p1 = new Properties(); |
| 67 | p1.setProperty("foo", "pineapple"); |
| 68 | p1.setProperty("bar", "lime"); |
| 69 | |
| 70 | Properties p2 = new Properties(); |
| 71 | p2.setProperty("bar", "grapefruit"); |
| 72 | p2.setProperty("baz", "blackberry"); |
| 73 | |
| 74 | |
| 75 | ImportConfig cfg = ConfigFactory |
| 76 | .create(ImportConfig.class, p1, p2); // props imported! |
| 77 | |
| 78 | assertEquals("pineapple", cfg.foo()); |
| 79 | |
| 80 | // p1 prevails, so this is lime and not grapefruit |
| 81 | assertEquals("lime", cfg.bar()); |
| 82 | |
| 83 | assertEquals("blackberry", cfg.baz()); |
| 84 | ``` |
| 85 | |
| 86 | This is pretty handy if you want to load properties provided by other mechanisms which not accessible through any of the supported URI schemes listed under [Loading Strategies](loading-strategies.md) |
| 87 | |
| 88 | For instance, a Java EE (a.k.a. Jakarta EE) servlet running on a servlet container might load properties during initialization from a resource accessible through its respective [ServletContext](https://javaee.github.io/javaee-spec/javadocs/javax/servlet/ServletContext.html): |
| 89 | |
| 90 | ```java |
| 91 | interface ServletContextProperties extends Config { |
| 92 | /** JDBC name of a data source used by the servlet */ |
| 93 | @Key("ds.name") |
| 94 | String dsName(); |
| 95 | |
| 96 | void list(PrintStream out); |
| 97 | } |
| 98 | |
| 99 | ... |
| 100 | |
| 101 | public class MyServlet extends HttpServlet { |
| 102 | |
| 103 | protected void init() { |
| 104 | ServletContextProperties cfg = ConfigFactory |
| 105 | .create(ServletContextProperties.class, |
| 106 | getServletConfig().getServletContext().getResourceAsStream("/WEB-INF/myServlet.properties")); |
| 107 | } |
| 108 | </div> |
| 109 | } |
| 110 | ``` |
| 111 | |
| 112 | <div class="note info"> |
| 113 | Note that this way of proceeding yields the responsibility of proper usage to the client, |
| 114 | whose code shall never 'forget' to include the <tt>import</tt> parameter when calling the <tt>ConfigFactory.create()</tt> ). |
| 115 | </div> |
| 116 | |
| 117 | Thus, if you want to refer to properties provided by any of the mechanisms directly supported |
| 118 | by the `@Source` annotation, you should rather use them, as explained in [Loading strategies](/docs/loading-strategies/). |
| 119 | In particular, to refer to system properties or environment variables, |
| 120 | you can use (since version 1.0.10) `system:properties` or `system:env` (respectively). |
| 121 | |
| 122 | Other typical usage of importing properties might involve loading them from other sources directly |
| 123 | provided by the execution environment, e.g. [servlet context](https://javaee.github.io/javaee-spec/javadocs/javax/servlet/ServletContext.html) attributes, [context or servlet initialization parameters](https://docs.oracle.com/cd/E19226-01/820-7627/bnaes/index.html), [JNDI](https://docs.oracle.com/javase/tutorial/jndi/index.html) application environment resources (i.e. entries under `java:comp/env/`), [Java preferences](https://docs.oracle.com/javase/8/docs/technotes/guides/preferences/index.html), or any other environment-dependent property sources. However, none of these sources direcly provide an API to access their contents as a `Map` object; |
| 124 | hence the programmer would need in that case to implement first their own method to convert from lists of names |
| 125 | plus individual values to `Map` object (therefore compatible with Owner API). |
| 126 | </div> |
| 127 | |
| 128 | Interactions with loading strategies |
| 129 | ------------------------------------ |
| 130 | |
| 131 | Notice that the "importing properties" feature is additional to the properties |
| 132 | loading mechanism explained in chapter |
| 133 | [Loading strategies](/docs/loading-strategies/). |
| 134 | |
| 135 | Properties imported programmatically have higher priority regarding the |
| 136 | properties loaded from the `@Sources` attribute. |
| 137 | |
| 138 | Imagine the scenario where the you define your configuration with `@Sources` |
| 139 | annotation, but you want to allow the user to specify a configuration file at |
| 140 | the command line. |
| 141 | |
| 142 | ```java |
| 143 | |
| 144 | @Sources(...) |
| 145 | interface MyConfig extends Config { |
| 146 | ... |
| 147 | } |
| 148 | |
| 149 | public static void main(String[] args) { |
| 150 | MyConfig cfg; |
| 151 | if (args.lenght() > 0) { |
| 152 | Properties props = new Properties(); |
| 153 | props.load(new FileInputStream(new File(args[0]))); |
| 154 | cfg = ConfigFactory.create(MyConfig.class, userProps); |
| 155 | } else { |
| 156 | cfg = ConfigFactory.create(MyConfig.class); |
| 157 | } |
| 158 | } |
| 159 | ``` |
| 160 | |
| 161 | In the above example, the properties file specified by the user will override |
| 162 | the properties loaded by `@Sources` if there is overlapping between the |
| 163 | properties names. |
| 164 | This approach is used by many command line tools, that allow the user to specify |
| 165 | a configuration on the command line that overrides the default one. |
| 166 | |
| 167 | <div class="note warning"> |
| 168 | <h5>This is true only with version 1.0.3.1 and superior!</h5> |
| 169 | <p>Be aware that in versions prior to 1.0.3.1 imported properties have lower |
| 170 | priority than others loaded properties. This behavior has been changed in |
| 171 | version 1.0.3.1 and it will be kept this way for future releases.</p> |
| 172 | </div> |
| 173 | |
| 174 |