Java 是否可以使用Spring和@Value注释将YAML属性读入地图

Java 是否可以使用Spring和@Value注释将YAML属性读入地图,java,spring,yaml,spring-el,Java,Spring,Yaml,Spring El,我希望能够做到的是: 亚马尔: 代码: @Value(“${features}”) 私有地图特征; 我不知道使用什么样的Spring脚本语法来实现这一点(如果可能的话)我正在使用Spring引导并访问如下自定义变量: features: feature1: true feature2: false feature3: true 创建映射到自定义属性的自定义类: @Component @ConfigurationProperties(prefix="features") publi

我希望能够做到的是:

亚马尔:

代码:

@Value(“${features}”)
私有地图特征;

我不知道使用什么样的Spring脚本语法来实现这一点(如果可能的话)

我正在使用Spring引导并访问如下自定义变量:

features:
  feature1: true
  feature2: false
  feature3: true
  • 创建映射到自定义属性的自定义类:

    @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}}"
    
  • 可能重复的
    "{{features.feature1}}"