sebastiandaschner blog


Map complex configuration structures with Quarkus

#quarkus saturday, july 17, 2021

In Quarkus you can easily inject configured values using @ConfigProperty. There’s also a way to inject nested and collection values using configuration objects. In the following video, I’ll show how:

 

 

In my example, we’re going to map the following nested configuration values into a Java type:

complex.coffee=coffee
complex.list[0]=123
complex.list[1]=234
complex.list[2]=456
complex.some-other.coffee=abc123
complex.some-other.list[0]=abc123
complex.some-other.list[1]=abc234
complex.some-other.list[2]=abc345

Before Quarkus version two there was a way to use the @ConfigProperties annotation, whereas now the recommended way is to use @ConfigMapping:

@ConfigMapping(prefix = "complex")
public interface ComplexConfiguration {

    String coffee();
    List<String> list();

    SomeOther someOther();

    interface SomeOther {
        String coffee();
        List<String> list();
    }
}

This ComplexConfiguration type can be injected as a bean using @Inject in our code.

It’s also possible, and sometimes helpful, to use a hierarchical format, such as YAML, to configure the values. The same example can be realized with a application.yaml config file, instead of application.properties:

complex:
  coffee: coffee
  list:
  - 123
  - 234
  - 456
  some-other:
    coffee: abc123
    list:
    - abc123
    - abc234
    - abc345

For this, you’ll need to add the quarkus-config-yaml dependency.

As always, you can find the code on GitHub.

For more information, have a look at the config mapping documentation.

And, if you want to learn more about modern development with Quarkus, have a look at my upcoming Quarkus workshops.

 

Found the post useful? Subscribe to my newsletter for more free content, tips and tricks on IT & Java: