gpt4 book ai didi

java - Spring 5 从环境中获取对象列表

转载 作者:行者123 更新时间:2023-12-02 11:46:15 25 4
gpt4 key购买 nike

在 Java 9 上使用最新的 Spring 5....

使用以下 YAML:

flow:
- name: cats
url: http://dogs.com
- name: dogs
url: http://cats.com

Environment可以照常提取嵌套属性值( env.getProperty("flow[0].name") 为字符串)。但如何将列表拉入 List<Flow>

假设我需要一个ConfigurationProperties映射到 Flow类(class)。不想在 yaml 中添加 flow 前缀。

然后通过Environment调用 getProperty 会发生什么?看起来像(例如 env.getProperty("flow", List.class) 但具有通用 List<Flow> 引用)。顺便说一句,我想要列表的原因是在使用单独的配置设置环境(即EnvironmentPostProcessor)后注册bean。

最佳答案

这应该有效。尝试一下。

@Configuration
@ConfigurationProperties
@Getter
@Setter
public class Configclass {

List<Flow> flow;
}

@Getter
@Setter
public class Flow {

public String name;
public String url;
}

关于java - Spring 5 从环境中获取对象列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48186080/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com