Spring: @NestedConfigurationProperty List in @ConfigurationProperties
Solution 1
- 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 propertymy.servers[X].server.name=test
Solution 2
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.
cloudnaut
Updated on June 29, 2022Comments
-
cloudnaut almost 2 years
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