• springboot读取配置文件中的集合对象


    配置文件如下:

    epic-category.properties

    static-epic-categories[0].static-epic-category-value.id=1
    static-epic-categories[0].static-epic-category-value.key=BUSINESS
    static-epic-categories[0].static-epic-category-value.name=业务类专题
    static-epic-categories[0].sub-categories[0].id=2
    static-epic-categories[0].sub-categories[0].key=SPORADIC_OPTIMIZATION_BUSINESS
    static-epic-categories[0].sub-categories[0].name=零星优化类(业务类)
    static-epic-categories[0].sub-categories[1].id=3
    static-epic-categories[0].sub-categories[1].key=SOLUTION_BUSINESS
    static-epic-categories[0].sub-categories[1].name=解决方案类(业务类)
    static-epic-categories[1].static-epic-category-value.id=4
    static-epic-categories[1].static-epic-category-value.key=TECH
    static-epic-categories[1].static-epic-category-value.name=技术类专题
    static-epic-categories[1].sub-categories[0].id=5
    static-epic-categories[1].sub-categories[0].key=SPORADIC_OPTIMIZATION_TECH
    static-epic-categories[1].sub-categories[0].name=零星优化类(技术类)
    static-epic-categories[1].sub-categories[1].id=6
    static-epic-categories[1].sub-categories[1].key=SOLUTION_TECH
    static-epic-categories[1].sub-categories[1].name=解决方案类(技术类)

    不难看出,配置文件中是多个对象集合,我们只有要建立对应的类进行读取即可。

    1.创建对应的实体类,建立映射并读取配置配置文件中的信息,使用时通过@Autowired注解注入即可

    import lombok.Data;
    import lombok.NoArgsConstructor;
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.context.annotation.PropertySource;
    import org.springframework.stereotype.Component;
    
    import java.util.List;
    
    /**
     * <p>
     *  读取epic-category.properties配置文件中的类型信息
     * </p>
     *
     * @className EpicCategory
     * @author Sue
     * @create 2020/9/30 
     **/
    @Data
    @NoArgsConstructor
    @Component
    @ConfigurationProperties(prefix = "")
    //不使用这个注解默认读取application.yml
    @PropertySource(value = "classpath:epic-category.properties")
    public class EpicCategory {
    
        private List<StaticEpicCategories> staticEpicCategories;
    
        @Data
        static class StaticEpicCategories {
            private StaticEpicCategoryValue staticEpicCategoryValue;
            private List<SubCategories> subCategories;
        }
    
        @Data
        static class StaticEpicCategoryValue {
            private String id;
            private String key;
            private String name;
        }
    
        @Data
        static class SubCategories {
            private String id;
            private String key;
            private String name;
        }
    
    
    }

    2.测试

    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class LeanManageApplicationTests {
    
        @Autowired
        EpicCategory epicCategory;
        @Autowired
        FeatureCategory featureCategory;
    
        @Test
        public void contextLoads() {
            System.out.println(epicCategory);
            System.out.println(featureCategory);
        }
    }
    

      

  • 相关阅读:
    phpStudy The requested URL /web/index.php was not found on this server
    python yield 理解与用法
    python 高阶函数之 map
    python 高阶函数之 reduce
    java extends和implements区别
    SpringMVC中使用Interceptor拦截器顺序
    SpringMVC 拦截器使用说明
    java session创建与获取
    跨域解决方案
    nrm -- 一键切换npm源
  • 原文地址:https://www.cnblogs.com/sueyyyy/p/13753498.html
Copyright © 2020-2023  润新知