- Property Mapping
Although config values are originally text, you can use the config system’s built-in conversions or add your own to translate text into Java primitive types and simple objects (such as
Double) and to express parts of the config tree as complex types (List,Map, and custom types specific to your application). This section introduces how to use the built-in mappings and your own custom ones to convert to simple and complext types.
Converting Configuration to Simple Types
The Config class itself provides many conversions to Java types. See the JavaDoc for the complete list.
The methods which support Java primitive types and their related classes follow a common pattern. The examples in the table below deal with conversion to a boolean but the same pattern applies to many data types listed in the JavaDoc.
Assume a local variable has been assigned something like
Config config = Config.get("someKey");
// shortcut method
ConfigValue<Boolean> value = config.asBoolean();
// generic method (for any type)
ConfigValue<Boolean> value2 = config.as(Boolean.class);| Java type | Example usage 1 | |
|---|---|---|
boolean | boolean b = value.get(); 2 | boolean defaultedB = value.orElse(true); 3 |
Optional<Boolean> | ConfigValue already has all methods of an Optional. If actual optional is needed: Optional<Boolean> b = value.asOptional(); 4 | |
Supplier<Boolean> | Boolean b = value.supplier().get(); | boolean defaultedB = value.supplier(true).get(); |
Supplier<Optional<Boolean>> | Boolean b = value.optionalSupplier().get().orElse(Boolean.TRUE); |
Notes on Built-in Conversions to Simple Types
1 All conversions can throw MissingValueException (if no value exists at the requested key and no default is provided) and ConfigMappingException (if some error occurred while performing the data mapping).
2 The Config.asXXX methods internally use the Java-provided XXX.parseXXX methods, so here a missing or unparseable string gives false because that is how Boolean.parseBoolean behaves.
3 User code defaults the value to true.
4 User code defaults the value to Boolean.TRUE if absent; otherwise parses the value using Boolean.parseBoolean.
The numerous conversions defined on the Config class for other types (integers, doubles, etc.) will satisfy many of your application’s needs. The ConfigMappers class includes other related mappings from String (rather than from Config) to Java types (described in the JavaDoc).
For additional type mapping, you can use these methods defined on Config:
T as(Class<? extends T> type);
T as(Function<Config, T> mapper);
T as(GenericType<T> genericType);which maps the current node to a type.
The next example, and later ones below showing complex type mapping, use the example application.properties configuration from the config introduction. Part of that example includes this line:
bl.initial-id = 10000000000Your application can use Config.as to interpret the value as a BigDecimal:
BigDecimal initialId = config.get("bl.initial-id").as(BigDecimal.class);Converting Configuration to Complex Types
The hierarchical features section describes the tree structure used to represent config data. The config system can map subtrees of a config tree to complex Java types.
Built-in Conversions to List and Map
The Config class exposes several methods for mapping a structured config node to a Java List or Map. The JavaDoc contains complete details, but briefly your application can convert a structured Config node into:
a
List<T>of a given type,a
Map<String, String>in which each key is the fully-qualified keyStringfor a config entry and the value is itsStringvalue, or
Custom Conversions
Often your code will be simpler if you can treat parts of the configuration as custom, application-specific Java objects, rather than as a group of String keys and values. You will need customized conversions to do so.
The config system provides many ways to accomplish this, described in the io.helidon.config package JavaDoc.
Some of those approaches require that the target class — the class to which you want to convert the configuration data — have certain characteristics or that you add a method to the class to help do the mapping. You might want to avoid changing the target class or you might not even be able to if you do not control its source.
Here are two approaches that will always work without requiring changes to the target class. For both approaches, you write your own conversion function. The difference is in how your application triggers the use of that mapper.
Use Custom Mapper Explicitly: Config.as method
Any time your application has a Config instance to map to the target class it invokes Config.as passing an instance of the corresponding conversion function:
Config config = Config.get("web");
ConfigValue<WebConfig> web = config.as(WebConfigMapper::map);You do not necessarily need a new instance of the mapper every time you want to use it.
In this approach, everywhere your application needs to perform this conversion it specifies the mapper to use. If you decided to change which mapper to use you would need to update each of those places in your application.
Register Custom Mapper Once, Use Implicitly: Config.as method
In this approach, your application:
- Tells each
Config.Builderthat needs to know about the custom mapper by either:- registering an instance of your mapper by invoking
Config.Builder.addMapper, or - implementing
ConfigMapperProviderso it returns an instance of your mapper (see the JavaDoc for complete information) and creating or editing the fileio.helidon.config.spi.ConfigMapperProviderso it contains a line with the fully-qualified class name of yourConfigMapperProvider. The config system will use the Java service loader to find and invoke allConfigMapperProviderclasses listed and add the mappers they provide to eachConfig.Builderautomatically.
- registering an instance of your mapper by invoking
- Converts using the mapper by invoking the
Config.asmethod which accepts the target type to convert to, not the mapper itself that does the conversion.
If your application converts to the same target type in several places in the code, this approach allows you to change which mapper it uses by changing only the registration of the mapper, not each use of it.
Continuing the Web Example
The following examples build on the example configuration from the application.properties example file in the introduction.
web Properties Configpublic class WebConfig {
private boolean debug;
private int pageSize;
private double ratio;
public WebConfig(boolean debug, int pageSize, double ratio) {
this.debug = debug;
this.pageSize = pageSize;
this.ratio = ratio;
}
public boolean isDebug() {
return debug;
}
public int getPageSize() {
return pageSize;
}
public double getRatio() {
return ratio;
}
}public class WebConfigMapper implements Function<Config, WebConfig> {
@Override
public WebConfig apply(Config config) throws ConfigMappingException, MissingValueException {
return new WebConfig(
config.get("debug").asBoolean().orElse(false),
config.get("page-size").asInt().orElse(10),
config.get("ratio").asDouble().orElse(1.0)
);
}
}...
Config config = Config.create(classpath("application.properties"));
WebConfig web = config.get("web")
.as(new WebConfigMapper())
.get();...
Config config = Config.builder(classpath("application.properties"))
.addMapper(WebConfig.class, new WebConfigMapper())
.build();
WebConfig web = config.get("web")
.as(WebConfig.class)
.get();Either of the two approaches just described will always work without requiring you to change the POJO class.
Advanced Conversions using Explicit Mapping Logic
If the target Java class you want to use meets certain conditions — or if you can change it to meet one of those conditions — you might not need to write a separate mapper class. Instead, you add the mapping logic to the POJO itself in one of several ways and the config system uses Java reflection to search for those ways to perform the mapping.
Your application facilitates this implicit mapping either by adding to the POJO class or by providing a builder class for it.
This feature is available in Object mapping module, and is added through Java ServiceLoader mechanism. This is no longer part of core Config module, as it depends on reflection and introduces a lot of magic (see the list of supported mapping methods below, also uses reflection to invoke the methods and to map configuration values to fields/methods etc.).
pom.xml<dependencies>
<dependency>
<groupId>io.helidon.config</groupId>
<artifactId>helidon-config-object-mapping</artifactId>
</dependency>
</dependencies>Adding the Mapping to the POJO
If you can change the target class you can add any one of the following methods or constructors to the POJO class which the config system will find and use for mapping.
static WebConfig create(Config); |
static WebConfig from(Config); |
static WebConfig from(String); |
static WebConfig of(Config); |
static WebConfig of(String); |
static WebConfig valueOf(Config); |
static WebConfig valueOf(String); |
static WebConfig fromConfig(Config); |
static WebConfig fromString(String); |
WebConfig(Config); |
WebConfig(String); |
If the config system finds any of these methods or constructors when the application invokes
WebConfig wc = config.as(WebConfig.class).get();it will invoke the one it found to map the config data to a new instance of the target class. You do not need to write a separate class to do the mapping or register it with the Config.Builder for the config instance.
Writing a Builder Method and Class for the POJO
You can limit the changes to the POJO class by adding a single builder method to the POJO which returns a builder class for the POJO:
public class WebConfig {
...
static WebConfigBuilder builder() {
return new WebConfigBuilder();
}
...
}The builder class WebConfigBuilder is expected to be a Java Bean with
- bean properties named for the config properties of interest, and
- a method
WebConfig build()which creates the mapped instance from the builder’s own bean properties.
When your application invokes config.as(WebConfig.class) the config system
- finds and invokes the
WebConfig.builder()method, - assigns the bean properties on the returned builder from the config subtree rooted at
config, and - invokes the builder’s
build()method yielding the resultingWebConfiginstance.
Conversions using JavaBean Deserialization
The config system can also interpret your classes as JavaBeans and use the normal bean naming conventions to map configuration data to your POJO classes, using one of these patterns:
- POJO as JavaBean - The config system treats the target class itself as a JavaBean, assigning values from the config to the bean properties of the POJO class.
- builder as JavaBean - The config system invokes the POJO’s
builder()method to obtain a builder for that POJO type and treats the builder class as a JavaBean, assigning values from the config to the builder’s bean properties and then invoking the builder’sbuildmethod to create an instance of the target POJO class. - POJO with factory method or decorated constructor - The config system finds a
frommethod or a constructor on the POJO class itself which accepts annotated arguments, then invokes that method or constructor passing the specified arguments based on the config. Thefrommethod returns an instance of the POJO class initialized with the values passed as arguments.
The following sections describe these patterns in more detail.
This feature is available in Object mapping module, and is added through Java ServiceLoader mechanism. This is no longer part of core Config module, as it depends on reflection.
pom.xml<dependencies>
<dependency>
<groupId>io.helidon.config</groupId>
<artifactId>helidon-config-object-mapping</artifactId>
</dependency>
</dependencies>POJO as JavaBean
If your POJO target class is already a JavaBean — or you can modify it to become one — you might be able to avoid writing any explicit mapping code yourself.
The config system invokes the no-args constructor on the target class to create a new instance. It treats each public setter method and each public non-final field as a JavaBean property. The config system processes any non-primitive property recursively as a JavaBean. In this way the config system builds up the target object from the config data.
By default, the system matches potential JavaBean property names with config keys in the configuration.
Use the Value annnotation to control some of the JavaBean processing for a given property.
Value Annotation| Attribute | Usage |
|---|---|
key | Indicates which config key should match this JavaBean property |
withDefault | String used for the bean property default value if none is set in the config |
withDefaultSupplier | Supplier of the default bean property value if nont is set in the config |
To exclude a bean property from the config system bean processing annotate it with Config.Transient.
Here is an example using the app portion of the example configuration from the introduction.
app propeties into via setterspublic class AppConfig {
private Instant timestamp;
private String greeting;
private int pageSize;
private List<Integer> basicRange;
public AppConfig() {
}
public void setGreeting(String greeting) {
this.greeting = greeting;
}
public String getGreeting() {
return greeting;
}
@Value(key = "page-size",
withDefault = "10")
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getPageSize() {
return pageSize;
}
@Value(key = "basic-range",
withDefaultSupplier = BasicRangeSupplier.class)
public void setBasicRange(List<Integer> basicRange) {
this.basicRange = basicRange;
}
public List<Integer> getBasicRange() {
return basicRange;
}
@Config.Transient
public void setTimestamp(Instant timestamp) {
this.timestamp = timestamp;
}
public Instant getTimestamp() {
return timestamp;
}
public static class BasicRangeSupplier
implements Supplier<List<Integer>> {
@Override
public List<Integer> get() {
return List.of(-10, 10);
}
}
}- Public no-parameter constructor.
- Property
greetingis not customized and will be set from the config node with the keygreeting, if present in the config. - Property
pageSizeis matched to the config keypage-size. - If the
page-sizeconfig node does not exist, thepageSizebean property defaults to10. - Property
basicRangeis matched to the config keybasic-range. - If the
basic-rangeconfig node does not exist, aBasicRangeSupplierinstance will provide the default value. - The
timestampbean property is never set, even if the config contains a node with the keytimestamp. BasicRangeSupplieris used to supply theList<Integer>default value.
Here is an example of code loading config and mapping part of it to the AppConfig bean above.
app config node into AppConfig classConfig config = Config.create(classpath("application.conf"));
AppConfig app = config.get("app")
.as(AppConfig.class)
.get();
//assert that all values are loaded from file
assert app.getGreeting().equals("Hello");
assert app.getPageSize() == 20;
assert app.getBasicRange().size() == 2
&& app.getBasicRange().get(0) == -20
&& app.getBasicRange().get(1) == 20;
//assert that Transient property is not set
assert app.getTimestamp() == null; - The config system finds no registered
ConfigMapperforAppConfigand so applies the JavaBean pattern to convert the config to anAppConfiginstance. - Because the bean property
timestampwas marked as transient, the config system did not set it.
Builder as JavaBean
If the target class includes the public static method builder() that returns any object, then the config system will make sure that the return type has a method build() which returns an instance of the target class. If so, the config system treats the builder as a JavaBean and
- invokes the
builder()method to instantiate the builder class, - treats the builder as a JavaBean and maps the
Configsubtree to it, - invokes the builder’s
build()method to create the new instance of the target class.
You can augment the target class with the public static builder() method:
app properties, via a Builderpublic class AppConfig {
private String greeting;
private int pageSize;
private List<Integer> basicRange;
private AppConfig(String greeting, int pageSize, List<Integer> basicRange) {
this.greeting = greeting;
this.pageSize = pageSize;
this.basicRange = basicRange;
}
public String getGreeting() {
return greeting;
}
public int getPageSize() {
return pageSize;
}
public List<Integer> getBasicRange() {
return basicRange;
}
public static Builder builder() {
return new Builder();
}
public static class Builder {
private String greeting;
private int pageSize;
private List<Integer> basicRange;
private Builder() {
}
public void setGreeting(String greeting) {
this.greeting = greeting;
}
@Value(key = "page-size",
withDefault = "10")
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
@Value(key = "basic-range",
withDefaultSupplier = BasicRangeSupplier.class)
public void setBasicRange(List<Integer> basicRange) {
this.basicRange = basicRange;
}
public AppConfig build() {
return new AppConfig(greeting, pageSize, basicRange);
}
}
}- The target class’s constructor can be
privatein this case because new instances are created from the inner classBuilderwhich has access to `AppConfig’s private members. - The target class contains
public staticmethodbuilder()which returns an object that itself exposes the methodAppConfig build(), so the config system recognizes it. - The config system treats the
AppConfig.Builder(not the enclosing target class) as a JavaBean. - The builder’s property
greetingis not customized and is set from config node withgreetingkey, if one exists. - The builder’s property
pageSizemaps to the config keypage-sizeand defaults to10if absent. - The builder’s property
basicRangemaps to the config keybasic-rangeand uses aBasicRangeSupplierinstance to get a default value if needed. - Finally, the config system invokes the builder’s public method
build(), creating the new instance ofAppConfigfor use by the application.
Target Class with Annotated Factory Method or Constructor
Another option is to annotate the parameters to a factory method or to a constructor on the target class. You can add a factory method to the target class, a public static method from with parameters annotated to link them to the corresponding config keys. Or you can add or modify a constructor with parameters, similarly annotated to form the link from each parameter to the corresponding config key.
Warning
Be sure to annotate each parameter of the from method or constructor with @Value and specify the key to use for the mapping. The parameter names in the Java code are not always available at runtime to map to config keys. (They might be arg0, arg1, etc.)
frompublic class AppConfig {
private final String greeting;
private final int pageSize;
private final List<Integer> basicRange;
private AppConfig(String greeting, int pageSize, List<Integer> basicRange) {
this.greeting = greeting;
this.pageSize = pageSize;
this.basicRange = basicRange;
}
public String getGreeting() {
return greeting;
}
public int getPageSize() {
return pageSize;
}
public List<Integer> getBasicRange() {
return basicRange;
}
public static AppConfig from(
@Value(key = "greeting")
String greeting,
@Value(key = "page-size",
withDefault = "10")
int pageSize,
@Value(key = "basic-range",
withDefaultSupplier = BasicRangeSupplier.class)
List<Integer> basicRange) {
return new AppConfig(greeting, pageSize, basicRange);
}
}- The target class constructor can be
privatebecause the factory method on the same class has access to it. - The config system invokes the factory method
from(…), passing arguments it has fetched from the correspondingly-named config subtrees. The factory method returns the new initializedAppConfiginstance. Note the consistent use of@Value(key = "…")on each parameter. - Because the property
greetingdoes not specify a default value the property is mandatory and must appear in the configuration source. Otherwise the config system throws aConfigMappingException.
Alternatively, you can use an annotated constructor instead of a static factory method. Revising the example above, make the constructor public, annotate its parameters, and remove the now-unneeded from factory method.
public class AppConfig {
...
public AppConfig(
@Value(key = "greeting")
String greeting,
@Value(key = "page-size",
withDefault = "10")
int pageSize,
@Value(key = "basic-range",
withDefaultSupplier = BasicRangeSupplier.class)
List<Integer> basicRange) {
this.greeting = greeting;
this.pageSize = pageSize;
this.basicRange = basicRange;
}- Constructor is
public. - Each parameter has the
ConfigValueannotation to at least specify the config key name.
When the application invokes config.as(AppConfig.class), the config system locates the public annotated constructor and invokes it, passing as arguments the data it fetches from the configuration matching the annotation key names with the configuration keys.