Programmatic access to properties created by property-placeholder

后端 未结 9 2399
长情又很酷
长情又很酷 2020-11-29 20:21

I\'m reading properties file using context:property-placeholder. How can I access them programatically (@Value doesn\'t work - I d

9条回答
  •  野性不改
    2020-11-29 21:13

    @Value
    

    annotation works on new releases of Spring (tested on v3.2.2) Here is how it is done:

    1. Map your properties file in spring configuration file

      
      
      
      
    2. Create app-config.properties inside (root) your source folder

      my.property=test
      my.property2=test2
      
    3. Create a controller class

      @Controller
      public class XRDSBuilder
      {
          @Value("${my.property}")
          private String myProperty;
      
          public String getMyProperty() { return myProperty; }
      }
      

    Spring will automatically map the content of my.property to your variable inside the controller

    Mapping to a list

    Property value:

    my.list.property=test,test2,test3
    

    Controller class configuration:

    @Value("#{'${my.list.property}'.split(',')}")
    private List myListProperty;
    

    Advanced mapping

    @Component("PropertySplitter")
    public class PropertySplitter {
    
        /**
         * Example: one.example.property = KEY1:VALUE1,KEY2:VALUE2
         */
        public Map map(String property) {
            return this.map(property, ",");
        }
    
        /**
         * Example: one.example.property = KEY1:VALUE1.1,VALUE1.2;KEY2:VALUE2.1,VALUE2.2
         */
        public Map> mapOfList(String property) {
            Map map = this.map(property, ";");
    
            Map> mapOfList = new HashMap<>();
            for (Entry entry : map.entrySet()) {
                mapOfList.put(entry.getKey(), this.list(entry.getValue()));
            }
    
            return mapOfList;
        }
    
        /**
         * Example: one.example.property = VALUE1,VALUE2,VALUE3,VALUE4
         */
        public List list(String property) {
            return this.list(property, ",");
        }
    
        /**
         * Example: one.example.property = VALUE1.1,VALUE1.2;VALUE2.1,VALUE2.2
         */
        public List> groupedList(String property) {
            List unGroupedList = this.list(property, ";");
    
            List> groupedList = new ArrayList<>();
            for (String group : unGroupedList) {
                groupedList.add(this.list(group));
            }
    
            return groupedList;
    
        }
    
        private List list(String property, String splitter) {
            return Splitter.on(splitter).omitEmptyStrings().trimResults().splitToList(property);
        }
    
        private Map map(String property, String splitter) {
            return Splitter.on(splitter).omitEmptyStrings().trimResults().withKeyValueSeparator(":").split(property);
        }
    }
    

    Property value:

    my.complex.property=test1:value1,test2:value2
    

    Controller class:

    @Value("#{PropertySplitter.map('${my.complex.property}')}")
    Map myComplexProperty;
    

提交回复
热议问题