我希望能够做的是:
YAML:
features:
feature1: true
feature2: false
feature3: true
代码:
@Value("${features}")
private Map<String,Boolean> features;
我无法弄清楚要使用什么 Spring 脚本语法来执行此操作(如果可能的话)
最佳答案
我正在使用 Spring Boot 并像这样访问自定义变量:
创建一个映射到您的自定义属性的自定义类:
@Component @ConfigurationProperties(prefix="features") public class ConstantProperties { private String feature1; public String getFeature1(){ return feature1; } public void setFeature1(String feature1) { this.feature1 = feature1; } }
YAML 文件将如下所示:
features: feature1: true feature2: false feature3: true
在您想要访问这些属性的类中,您可以使用以下方法:
@Autowire private ConfigurationProperties configurationProperties;
然后要访问该类,请使用以下语法:
configurationProperties.getFeature1();
或者您可以像这样引用自定义属性:
"{{features.feature1}}"
关于java - 是否可以使用 Spring 和 @Value 注释将 YAML 属性读入 Map,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46574149/