[英]Spring Boot: Map a map from application.yaml to a map with @Value annotation
[英]Is it possible to read YAML property into Map using Spring and @Value annotation
我希望能够做的是:
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}}"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.