Spring:@ConfigurationProperties 中的@NestedConfigurationProperty 列表
Spring: @NestedConfigurationProperty List in @ConfigurationProperties
您好,我正在尝试进行以下配置 运行。
@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;
}
所以,我想在对象中嵌套多个服务器配置。
我尝试使用以下属性文件设置属性。我可以看到列表是按项目添加的,但从未设置服务器的成员(名称、描述)
my.servers[0].name=test
my.servers[0].server.name=test
my.servers[1].name=test
my.servers[1].server.name=test
- 您需要将 setter 和 getter 添加到
ServerConfiguration
- 您不需要使用
@ConfigurationProperties
的嵌套属性注释 class
ServerConfiguration.description
和 属性 之间的名称不匹配 my.servers[X].server.name=test
扩展 Maciej 已经说过的内容。
@ConfigurationProperties
应该只在 root 对象上设置(即负责处理给定前缀的对象。不需要用它注释嵌套对象.
@NestedConfigurationProperty
仅 由元数据生成器使用(表示 属性 不是单个值,而是我们应该探索的东西生成额外的元数据。在 List
的情况下,没有任何有限数量的属性,因此元数据必须在列表中停止。
在任何情况下,每个单数 属性 都需要一个 getter 和一个 setter。我们不进行字段绑定,我们需要 public getter 以避免在元数据中暴露不必要的属性。
您好,我正在尝试进行以下配置 运行。
@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;
}
所以,我想在对象中嵌套多个服务器配置。 我尝试使用以下属性文件设置属性。我可以看到列表是按项目添加的,但从未设置服务器的成员(名称、描述)
my.servers[0].name=test
my.servers[0].server.name=test
my.servers[1].name=test
my.servers[1].server.name=test
- 您需要将 setter 和 getter 添加到
ServerConfiguration
- 您不需要使用
@ConfigurationProperties
的嵌套属性注释 class
ServerConfiguration.description
和 属性 之间的名称不匹配my.servers[X].server.name=test
扩展 Maciej 已经说过的内容。
@ConfigurationProperties
应该只在 root 对象上设置(即负责处理给定前缀的对象。不需要用它注释嵌套对象.
@NestedConfigurationProperty
仅 由元数据生成器使用(表示 属性 不是单个值,而是我们应该探索的东西生成额外的元数据。在 List
的情况下,没有任何有限数量的属性,因此元数据必须在列表中停止。
在任何情况下,每个单数 属性 都需要一个 getter 和一个 setter。我们不进行字段绑定,我们需要 public getter 以避免在元数据中暴露不必要的属性。