Mapping list in Yaml to list of objects in Spring Boot

后端 未结 6 815
暖寄归人
暖寄归人 2020-11-29 03:52

In my Spring Boot app I have application.yaml configuration file with following content. I want to have it injected as a Configuration object with list of channel configurat

6条回答
  •  执笔经年
    2020-11-29 04:31

    I had referenced this article and many others and did not find a clear cut concise response to help. I am offering my discovery, arrived at with some references from this thread, in the following:

    Spring-Boot version: 1.3.5.RELEASE

    Spring-Core version: 4.2.6.RELEASE

    Dependency Management: Brixton.SR1

    The following is the pertinent yaml excerpt:

    tools:
      toolList:
        - 
          name: jira
          matchUrl: http://someJiraUrl
        - 
          name: bamboo
          matchUrl: http://someBambooUrl
    

    I created a Tools.class:

    @Component
    @ConfigurationProperties(prefix = "tools")
    public class Tools{
        private List toolList = new ArrayList<>();
        public Tools(){
          //empty ctor
        }
    
        public List getToolList(){
            return toolList;
        }
    
        public void setToolList(List tools){
           this.toolList = tools;
        }
    }
    

    I created a Tool.class:

    @Component
    public class Tool{
        private String name;
        private String matchUrl;
    
        public Tool(){
          //empty ctor
        }
    
        public String getName(){
            return name;
        }
    
        public void setName(String name){
           this.name= name;
        }
        public String getMatchUrl(){
            return matchUrl;
        }
    
        public void setMatchUrl(String matchUrl){
           this.matchUrl= matchUrl;
        }
    
        @Override
        public String toString(){
            StringBuffer sb = new StringBuffer();
            String ls = System.lineSeparator();
            sb.append(ls);
            sb.append("name:  " + name);
            sb.append(ls);
            sb.append("matchUrl:  " + matchUrl);
            sb.append(ls);
        }
    }
    

    I used this combination in another class through @Autowired

    @Component
    public class SomeOtherClass{
    
       private Logger logger = LoggerFactory.getLogger(SomeOtherClass.class);
    
       @Autowired
       private Tools tools;
    
       /* excluded non-related code */
    
       @PostConstruct
       private void init(){
           List  toolList = tools.getToolList();
           if(toolList.size() > 0){
               for(Tool t: toolList){
                   logger.info(t.toString());
               }
           }else{
               logger.info("*****-----     tool size is zero     -----*****");
           }  
       }
    
       /* excluded non-related code */
    
    }
    

    And in my logs the name and matching url's were logged. This was developed on another machine and thus I had to retype all of the above so please forgive me in advance if I inadvertently mistyped.

    I hope this consolidation comment is helpful to many and I thank the previous contributors to this thread!

提交回复
热议问题