- The Configuration Component
The config component provides a Java API to load and process configuration properties in key/value form into a
Configobject which the application can use to retrieve config data.
Getting Started
Introducing the Config System
A brief overview of the config system helps clarify its different parts and how they work together. Most applications will typically deal with more than one of these parts.

The system reads configuration from a config source, a physical location (such as a file, a URL, or a String) which holds config data. Each config source works with a config parser which translates a particular text format (for example, Java properties or YAML) into an in-memory tree which represents the configuration’s structure and values. An optional polling strategy detects and publishes changes to the underlying config source so the config source itself or your application can respond.
Your application uses the Config object which results from building that in-memory tree to retrieve config data. The app can navigate explicitly among the nodes in the tree and fetch a node’s value
int pageSize = config
.get("web")
.get("page-size")
.asInt()
.orElse(20);or it can address a node in the tree using the config key’s dotted name
int pageSize = config
.get("web.page-size")
.asInt()
.orElse(20);As part of retrieving a value from a node, the config system applies config filters which can change what values are returned for selected keys.
The Config object lets your application retrieve config data as a typed ConfigValue.
ConfigValue<T> can be used to obtain: * an Optional<T> value from a single node, * the T value from a single node interpreted as a basic Java type (primitive or simple object) already known to the config system (such as a boolean or a Double), or * a complex Java type from a subtree of the config tree.
+ The config system automatically knows how to return List and Map complex types, and you can provide config mappers to convert a config subtree to whatever Java types your application needs.
Your First Config Application
An easy way to start with the Config API is to follow these four steps:
- add config-related dependencies to your
pom.xml - revise your
module-info.javato refer to config (if you are using Java 9) - create a simple config properties file
- retrieve and use the default
Configfrom your app
Add Maven Dependency on Config
pom.xml<dependencies>
<dependency>
<groupId>io.helidon.config</groupId>
<artifactId>helidon-config</artifactId>
<version>version-of-config-you-are-using</version>
</dependency>
</dependencies>Update module-info.java
If you are using Java 9 then create or update the module-info.java file for your application:
module-info.javamodule myModule {
requires io.helidon.config;
}Create simple Config Properties File
src/main/resources/application.properties config filegreeting = Hello
web.debug = true
web.page-size = 20
web.ratio = 1.3
bl.initial-id = 10000000000
origin = props
java.home=homeFromProps # will be ignoredWrite Code using the Default Config
Config from Javaimport io.helidon.config.Config;
...
Config config = Config.create();
System.out.println(String.format(
"greeting is %s\n"
+ "web.debug is %b\n"
+ "web.page-size is %d\n"
+ "web.ratio is %f\n"
+ "bl.initial-id is %d\n"
+ "origin is %s\n"
+ "java.home is %s",
config.get("greeting").asString().orElse("Default greeting"),
config.get("web.debug").asBoolean().orElse(false),
config.get("web.page-size").asInt().orElse(50),
config.get("web.ratio").asDouble().orElse(2.0),
config.get("bl.initial-id").asLong().orElse(1L),
config.get("origin").asString().orElse("defaults"),
config.get("java.home").asString().get())); - Import
Config. - Create the root of the
Configtree from the default sources. - Retrieve various values by their dotted names and decode them as the appropriate Java types, providing default values if the property is missing.
- Retrieve the value (and fail with a runtime exception if missing)
When you build and run your project, the output will look like this:
greeting is Hello
web.debug is true
web.page-size is 20
web.ratio is 1.300000
bl.initial-id is 10000000000
origin is props
java.home is /Library/Java/JavaVirtualMachines/jdk-10.0.1.jdk/Contents/HomeConfig Sources for the Default Config
The default config uses the following config sources, listed here from most to least important:
- Java system properties
- Environment variables
application.properties, if on the classpath.
The priority (most to least important) means that if a given config key appears in more than one source, the value assigned in a more important source overrules the value from a less important source.
Verify this by noting that the program has displayed your actual java.home which Java set as a system property, not the value set in the example application.properties file.
Built-in Support for Config Formats
If you add additional Helidon config maven artifacts to your dependencies, then the config system can read formats other than Java properties format and the default configuration will search for other application file types in the following order. Note that the default configuration stops once it finds one of the files below; it does not merge all such files it can find.
| Source | Helidon maven artifact ID (group ID: io.helidon.config) | Notes |
|---|---|---|
application.yaml | helidon-config-yaml | YAML format http://yaml.org |
application.conf | helidon-config-hocon | HOCON format https://github.com/lightbend/config#using-hocon-the-json-superset |
application.json | helidon-config-hocon | JSON format https://json.org/ |
application.properties | helidon-config | Java properties format |
Next Steps
Although the default configuration is very simple to use, your application can take as much control as it needs over
loading configuration data,
accessing the data once loaded, and
extending and modifying the behavior of the config system.
You do this by:
creating and invoking methods on a
Config.Builderobject to construct aConfiginstanceUsing a builder, the application can control everything about how the config system creates the resulting
Configinstance: config sources, parsers, polling strategy, filters, overrides, mappers, whether or not environment variables and Java system properties serve as config sources. The JavaDoc explains how to use theConfig.Builder.or
creating a meta-configuration file on the runtime classpath to control how the config system prepares the default configuration.
Once created, the Config object provides many methods the application can use to retrieve config data as various Java types. See the Config JavaDoc for complete details.
The links in the following tables lead you to more information about various other config topics.
| Topic | Documentation |
|---|---|
| Where config comes from | Config sources, meta-configuration |
| What format config data is expressed in | Config parsers, supported formats |
| How to filter, override, and dereference values | Filters and overrides |
| What happens when config data changes | Config polling |
| How to deal with loading errors | Config retry policies |
| Topic | Documentation |
|---|---|
| How config data is translated into Java types | Config mappers |
| How to navigate config trees | Navigation |
| Topic | Documentation |
|---|---|
| Writing extensions | Extensions |