zoukankan      html  css  js  c++  java
  • 读取配置文件参数转成对象,map集合。list集合

    1.   配置文件参数转成对象

            配置文件:properties形式

    soul.server.host=192.168.80.96
    soul.server.port=9999

           添加一个配置类:

    //方式一 使用@value 注意要使用 ${}包裹
    @Configuration
    @Data
    public class SoulConfig {
        @Value("${soul.server.host}")
        String host;
        @Value("${soul.server.port}")
        String port;
    }
    
    //方式二使用注解@configurationProperties 
    @configurationProperties("soul.server")
    @Configuration
    @Data
    public class SoulConfig {
        String host;
        String port;
    }

    2.配置文件转map集合

    soul.auth.map.2.appKey=99999999999999999999999999999
    soul.auth.map.2.appSecret=888888888888888888888888
    soul.auth.map.3.appKey=123123123123123
    soul.auth.map.3.appSecret=132123123123123

    #list 第一种方式
    soul.auth.list[0]=apple0
    soul.auth.list[1]=apple1
    soul.auth.list[2]=apple2
    #list 第二种方式
    soul.auth.list=apple0,apple1,apple2

    添加一个配置类:

    @Configuration
    @ConfigurationProperties(prefix = "soul.auth")
    @Data
    public class SoulAppConfig {
       //map集合    // 这里的属性map对应配置文件中的map ,放在yml文件其实更加一目了然 , map就是一个Map<Integer, AppKeyAndAppSecretEntity>的一个对象 Map
    <Integer, AppKeyAndAppSecretEntity> map;
    //list集合
         List<String> list;
    //这里是再外部通过一个叫sysId的参数来获取map集合中对应键的对象(AppKeyAndAppSecretEntity)值,将配置文件中的 2和3转成集合中的key 值对应就是对象AppKeyAndAppSecretEntity
       
     public AppKeyAndAppSecretEntity getAppKeyAndAppSecretEntity(Integer sysId){
            return map.get(sysId);
        }
    
    }
    
    
    //贴一下AppKeyAndAppSecretEntity对象 
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public class AppKeyAndAppSecretEntity {
        String appKey;
        String appSecret;
    }

    测试输出结果

    @Autowired
        SoulAppConfig soulAppConfig;
     @Test
        public  void http() {
    Map<Integer, AppKeyAndAppSecretEntity> map = soulAppConfig.getMap();
    
    }

     

  • 相关阅读:
    css 浮动元素居中
    盒子绝对定位 position:foxed ; 居中
    一些不常见的css知识
    apache 与php的安装
    dos命令
    php 数据库查询order by 与查询返回的数据类型
    最好花5分钟看一下:辞职后五险一金怎么办
    JDK神坑:JAVA中Calendar的月份Month少1
    三种代理服务器以及反向代理详解
    代理服务器基本知识普及代理IP使用方法!
  • 原文地址:https://www.cnblogs.com/yifachen/p/15601538.html
Copyright © 2011-2022 走看看