2017-10-04 105 views
0

我希望能够做的是:是否可以读取YAML财产到使用Spring和@Value标注地图

YAML:

features: 
    feature1: true 
    feature2: false 
    feature3: true 

代码:

@Value("${features}") 
private Map<String,Boolean> features; 

我无法弄清楚使用什么Spring脚本语法来完成此操作(如果可能的话)

+1

[春季启动的可能的复制 - 注入从application.yml映射](https://stackoverflow.com/questions/24917194/spring-boot-inject-map-from-application-yml) –

回答

0

我使用Spring Boot并访问了客户端m个变量是这样的:

  1. 创建一个映射到您的自定义属性的自定义类:

    @Component 
    @ConfigurationProperties(prefix="features") 
    public class ConstantProperties { 
        private String feature1; 
    
        public String getFeature1(){ 
         return feature1; 
        } 
        public void setFeature1(String feature1) { 
         this.feature1 = feature1; 
        } 
    } 
    
  2. YAML文件看起来像这样:

    features: 
        feature1: true 
        feature2: false 
        feature3: true 
    
  3. 你们班

    那要访问这些属性,可以使用以下内容:

    @Autowire 
    private ConfigurationProperties configurationProperties; 
    
  4. 然后给那个类的访问,请使用以下语法:

    configurationProperties.getFeature1(); 
    
  5. 或者你可以引用自定义属性,如:

    "{{features.feature1}}" 
    
相关问题