Debian Codegraph @

owner-site/site/docs/usage.md

raw symbols SCIP
1 ---
2 layout: docs
3 title: Basic usage
4 prev_section: installation
5 next_section: loading-strategies
6 permalink: /docs/usage/
7 ---
8
9 The approach used by OWNER APIs, is to define a Java interface associated to a
10 properties file.
11
12 Suppose your properties file is defined as `ServerConfig.properties`:
13
14 ```properties
15 port=80
16 hostname=foobar.com
17 maxThreads=100
18 ```
19
20 To access this properties file you need to define a convenient Java interface
21 `ServerConfig.java` in the same package:
22
23 ```java
24 import org.aeonbits.owner.Config;
25
26 public interface ServerConfig extends Config {
27 int port();
28 String hostname();
29 @DefaultValue("42")
30 int maxThreads();
31 }
32 ```
33
34 Notice that the above interface extends from `Config`, that
35 is a marker interface recognized by OWNER as valid to work with.
36
37 We'll call this interface the *Properties Mapping Interface* or just
38 *Mapping Interface* since its goal is to map Properties into a an easy to use
39 piece of code.
40
41
42 How does the mapping work?
43 --------------------------
44
45 Since the properties file does have the same name as the Java class, and they
46 are located in the same package, the OWNER API will be able to automatically
47 associate them.
48 For instance, if your *mapping interface* is called `com.foo.bar.ServerConfig`,
49 OWNER will try to associate it to `com.foo.bar.ServerConfig.properties`,
50 loading from the classpath.
51
52
53 The properties names defined in the properties file will be associated to the
54 methods in the Java class having the same name.
55 For instance, the property `port` defined in the properties file will be
56 associated to the method `int port()` in the Java class, the property `hostname`
57 will be associated to the method `String hostname()` and the appropriate type
58 conversion will apply automatically, so the method `port()` will return an int
59 while the method `hostname()` will return a Java string, since the interface is
60 defined in this way.
61
62 The mapping mechanism is fully customizable, as well the automatic type
63 conversion we just introduced is flexible enough to cover most of the Java types
64 as well as object types defined by the user.
65 You can see how in the next chapters.
66
67 ## Using the Config object
68
69 At this point, you can create the ServerConfig object and use it in your code:
70
71 ```java
72 ServerConfig cfg = ConfigFactory.create(ServerConfig.class);
73 System.out.println("Server " + cfg.hostname() + ":" + cfg.port() +
74 " will run " + cfg.maxThreads());
75 ```
76
77
78 Using @DefaultValue and @Key annotations
79 ----------------------------------------
80
81 Did you notice that in the above example it is specified `@DefaultValue("42")`
82 annotation?
83
84 ```java
85 public interface ServerConfig extends Config {
86 int port();
87 String hostname();
88 @DefaultValue("42") // here!!!
89 int maxThreads();
90 }
91 ```
92
93 It is used in case the `maxThread` key is missing from the
94 properties file.
95
96 This annotation gets automatically converted to `int`, since `maxThreads()`
97 returns an `int`.
98
99 Using the annotations, you can also customize the property keys:
100
101 ```properties
102 # Example of property file 'ServerConfig.properties'
103 server.http.port=80
104 server.host.name=foobar.com
105 server.max.threads=100
106 ```
107
108 This time, as commonly happens in Java applications, the properties names are
109 separated by dots. Instead of just "port" we have "server.http.port", so we
110 need to map this property name to the associated method using the `@Key`
111 annotation.
112
113 ```java
114 /*
115 * Example of ServerConfig.java interface mapping the previous
116 * properties file.
117 */
118 public interface ServerConfig extends Config {
119 @Key("server.http.port")
120 int port();
121
122 @Key("server.host.name")
123 String hostname();
124
125 @Key("server.max.threads");
126 @DefaultValue("42")
127 int maxThreads();
128 }
129 ```
130
131 The `@DefaultValue` and `@Key` annotations are the basics to start using the
132 OWNER API.
133
134 <div class="note">
135 <h5>You can leave the properties file away during development!</h5>
136 <p>
137 During the development you may decide to just use the `@DefaultValue` to
138 provide a default configuration, without really adding the properties file.
139 You can add the properties file later or leave this task to the end user.
140 </p>
141 </div>
142
143 Undefined properties
144 --------------------
145
146 Suppose you have defined a method in your *mapping interface* that cannot be
147 resolved to any property loaded from a properties file, and this method doesn't
148 define a `@DefaultValue` what happens? Simple: it will return null, or a
149 NullPointerException;
150
151 Suppose our ServerConfig class was looking like this:
152
153 ```java
154 public static interface ServerConfig extends Config {
155 String hostname();
156 int port();
157 Boolean debugEnabled();
158 }
159 ```
160
161 If we don't have any ServerConfig.properties associated to it, when we call the
162 method `String hostname()` it will return null, as well as when we call the
163 method `Boolean debugEnabled()` since the return types String and Boolean are
164 java objects. But if we call the method `int port()` then a
165 `NullPointerException` will be raised.
166
167 <div class="note">
168 <h5>You don't want the NullPointerException?</h5>
169 <p>
170 If you don't want to get the NullPointerException, you can just define
171 a default value. For instance, you can set <code>@DefaultValue("0")</code> for
172 an <code>int</code> return type, or a <code>@DefaultValue("false")</code> for a
173 <code>boolean</code> return type, and so on...
174 </p>
175 </div>
176
177 Conclusions
178 -----------
179
180 Now you know the minimum to get productive with the OWNER API. But this is just
181 the beginning. OWNER is a rich API that allows you to add additional behaviors
182 and have more interesting features, so that you should be able to use this
183 library virtually in any other context where you where using the
184 `java.util.Properties` class.