Search code examples
javamicronaut

How do I read Micronaut array properties from the root of the configuration hierarchy?


If I have a config file

app:
    integers:
        - 1
        - 2

I can use Micronaut's ConfigurationProperties to create config beans:

@ConfigurationProperties("app")
public class AppConfig {
  public List<Integer> integers;
}

But what if the integers array is in the root of the config hierarchy:

integers:
    - 1
    - 2

The following does not seem to work:

@ConfigurationProperties("")
public class RootConfig {
  public List<Integer> integers;
}

Solution

  • @ConfigurationProperties only works with a prefix. And "" is not a valid prefix.

    Moreover @ConfigurationProperties makes sense only if you have many fields(May be that is why it was not designed to work) not for one field like

    integers:
        - 1
        - 2
    

    In this case @ConfigurationProperties is a overkill. Just use @Value like this

    @Value("${integers}")
    public List<Integer> integers;
    

    and declare it like this in yaml

    integers: 1,2