Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring: @NestedConfigurationProperty List in @ConfigurationProperties

Hi I am trying to get the following configuration up and running.

@ConfigurationProperties(prefix="my")
public class Config {

    @NestedConfigurationProperty
    private List<ServerConfiguration> servers = new ArrayList<ServerConfiguration>();

    public List<ServerConfiguration> getServers() {
        return this.servers;
    }
}

@ConfigurationProperties(prefix = "server")
public class ServerConfiguration {
    private String name;
    private String description;
}

So, I want to have multiple server configs nested in objects. I tried setting the properties with the following properties file. I can see that the list is added up by items but the members of the server are never set (name, description)

my.servers[0].name=test
my.servers[0].server.name=test
my.servers[1].name=test
my.servers[1].server.name=test
like image 928
cloudnaut Avatar asked Jun 22 '15 16:06

cloudnaut


2 Answers

To extend what Maciej said already.

@ConfigurationProperties should be set only on root objects (that is objects that are responsible to handle a given prefix. There is no need to annotated nested objects with it.

@NestedConfigurationProperty is only used by the meta-data generator (to indicate that a property is not a single value but something we should explore to generate additional meta-data. In the case of the List there isn't any finite amount of properties so the meta-data will have to stop at the list.

In any case you need a getter and a setter for each singular property. We don't do field binding and we require a public getter to avoid exposing unnecessary properties in the meta-data.

like image 90
Stephane Nicoll Avatar answered Sep 21 '22 16:09

Stephane Nicoll


  • You need to add setters and getters to ServerConfiguration
  • You don't need to annotate class with nested properties with @ConfigurationProperties
  • There is a mismatch in names between ServerConfiguration.description and property my.servers[X].server.name=test
like image 20
Maciej Walkowiak Avatar answered Sep 23 '22 16:09

Maciej Walkowiak