Is it possible to read YAML property into Map using Spring and @Value annotation

夙愿已清 提交于 2019-12-06 05:01:43
Steven

I'm using Spring Boot and access custom variables like this:

  1. Create a custom class that maps to your custom properties:

    @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 file will look like this:

    features:
      feature1: true
      feature2: false
      feature3: true
    
  3. in your class that you want to access these properties, you can use the following:

    @Autowire 
    private ConfigurationProperties configurationProperties;
    
  4. Then to access in that class, use the following syntax:

    configurationProperties.getFeature1();
    
  5. Or you can reference the custom property like:

    "{{features.feature1}}"
    
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!