码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • springboot2.0 读取yml 配置 array,list,map,单值,及其组合


    文章目录

    • 1. yml key特殊符号处理
    • 2. yml @Value 属性配置
      • 基本类型
      • 数组 Array
      • List
      • Set 去重
      • Map
    • 3. yml @ConfigurationProperties 读取配置
      • 普通属性配置
      • Map 数据结构配置
    • 4. 参考文档

    1. yml key特殊符号处理

    如果key中存在特殊字符如:/、.、*等,我们可以使用"[]"进行转义

    app:
      "[/api/**]": anno
    
    • 1
    • 2

    2. yml @Value 属性配置

    基本类型

    spring:
      profiles:
        active: dev
    
    typechange:
      turnOn: 1
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    java

    /**
     * 部署环境
     */
    @Value("${spring.profiles.active:prod}")
    private String env;
    
    /**
     * 1:开启 0:关闭
     */
    @Value("${typechange.turnOn}")
    private Integer turnOn;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    数组 Array

    test:
      array1: aaa,bbb,ccc
      array2: 111,222,333
      array3: 11.1,22.2,33.3
    
    • 1
    • 2
    • 3
    • 4

    java

    // : 冒号后的值表示当 key 不存在时候使用的默认值,使用默认值时数组的 length = 0
    
    @Value("${test.array1:}")
    private String[] testArray1;
    
    @Value("${test.array2:}")
    private int[] testArray2;
    
    @Value("${test.array3:}")
    private double[] testArray3;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    List

    test:
      list: aaa,bbb,ccc
    
    • 1
    • 2

    java

    // yml必须要有此配置,不然启动会报错
    @Value("#{'${test.list}'.split(',')}")
    private List testList;
    
    //加默认值并判断集合非空,避免不配置这个 key 时候程序报错
    @Value("#{'${test.list:}'.empty ? null : '${test.list:}'.split(',')}")
    private List testList;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    Set 去重

    test:
      set: 111,222,333,111
    
    • 1
    • 2

    java

    @Value("#{'${test.set:}'.empty ? null : '${test.set:}'.split(',')}")
    private Set testSet;
    
    // output: [111, 222, 333]
    
    • 1
    • 2
    • 3
    • 4

    Map

    yml配置

    test:
      map1: '{"name": "zhangsan", "sex": "male"}'
      map2: '{"math": "90", "english": "85"}'
    
    • 1
    • 2
    • 3

    Java

    @Value("#{${test.map1}}")
    private Map map1;
    
    @Value("#{${test.map2}}")
    private Map map2;
    
    • 1
    • 2
    • 3
    • 4
    • 5

    注意,使用这种方式,必须得在配置文件中配置该 key 及其 value。我在网上找了许多资料,都没找到利用 EL 表达式支持不配置 key/value 的写法。如果你真的很需要这个功能,就得自己写解析方法了,这里以使用 fastjson 进行解析为例:
    (1) 自定义解析方法

    public class MapDecoder {
        public static Map decodeMap(String value) {
            try {
                return JSONObject.parseObject(value, new TypeReference>(){});
            } catch (Exception e) {
                return null;
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    java使用

    @Value("#{T(com.github.jitwxs.demo.MapDecoder).decodeMap('${test.map1:}')}")
    private Map map1;
    
    @Value("#{T(com.github.jitwxs.demo.MapDecoder).decodeMap('${test.map2:}')}")
    private Map map2;
    
    • 1
    • 2
    • 3
    • 4
    • 5

    3. yml @ConfigurationProperties 读取配置

    普通属性配置

    person:
      lastName: hello
      age: 18
      boss: false
      birth: 2017/12/12
      maps: 
        k1: v1
        k2: 12
      lists:
        - lisi
        - zhaoliu
      dog:
        name: 小狗
        age: 12
      varmaplist:
        key11:
          - t1
          - t2
          - t3
        key22:
          - t11
          - t22
          - t33
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    java用法:

    @Component
    @ConfigurationProperties(prefix = "person")
    @Data
    public class Person {
     
        private String lastName;
        
        private Integer age;
        
        private Boolean boss;
        
        private Date birth;
     
        private Map maps;
        
        private List lists;
        
        private Dog dog;
        
        private Map> varmaplist;
    
    }    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    Map 数据结构配置

    systems:
      systemInfo:
        "[http://test.mdm.bop.com]": #CMC
          uiUrl: http://test.mdm.bop.com/cmc-portal
          notLoginUrls: cmc-portal/api/**,cmc-portal/index,cmc-portal/logout
          notAclUrls: cmc-portal/login,cmc-portal/link/rest/getMenuList,cmc-portal/link/rest/getRoleList
          indexUrl: cmc-portal/index
          logoutUrl: cmc-portal/logout
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    java 用法:

    /**
     * 系统公共配置
     *
     * @author zhaoyang10
     * @date 2020/7/16
     */
    @RefreshScope
    @Component
    @ConfigurationProperties(prefix = "systems")
    @Data
    public class NacosSystemConfig {
    
        private Map systemInfo;
    }
    
    /**
     * 系统配置项实体类
     *
     * @author zhaoyang10
     * @date 2020/7/16
     */
    @Data
    public class SystemInfo {
    
        /**
         * 域名
         */
        private String domain;
    
        /**
         * 前端url
         */
        private String uiUrl;
    
        /**
         * 不需要登录的url
         */
        private String notLoginUrls;
    
        /**
         * 需要登录但不需要鉴权的url
         */
        private String notAclUrls;
    
        /**
         * 只需要weibo登录的url
         */
        private String weiboLoginUrls;
    
        /**
         * 登录策略
         */
        private String loginStrategy;
    
        /**
         * 鉴权url
         */
        private String authRemoteUrl;
    
        /**
         * 登录url
         */
        private String indexUrl;
    
        /**
         * 登出url
         */
        private String logoutUrl;
    
        /**
         * cas/sso登录页url
         */
        private String signoutUrl;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74

    4. 参考文档

    spring boot 读取配置文件

    spring boot yml配置注入 占位符配置

    spring boot 配置文件详解 @ConfigurationProperties @Value区别

    spring boot 读取配置文件(yml)中自定义的集合数组

    springboot yml配置list、map组合写法

    Spring boot2:Profile使用

    SpringBoot启动时如何对配置文件进行校验?

  • 相关阅读:
    壹基金为爱同行到余村,以一步步健行换一滴滴净水
    TRC心血管研究之艾美捷TRC心肌梗塞研究领域
    TCP三次握手和四次挥手基本知识
    【Python小项目之Tkinter应用】随机点名/抽奖工具小优化:实现输入框人数限定与人名显示优化,保证结果人名在窗口内显示,如果内容显示超出则弹出警告窗口
    开源原生android的视频编辑软件
    真人踩过的坑,告诉你避免自动化测试常犯的10个错误
    安卓开发面试题
    SpriteShapeProfile
    【2017年数据结构真题】
    合创汽车V09纵享商务丝滑?预售价32万元起,正式宣布大规模生产
  • 原文地址:https://blog.csdn.net/m0_67403076/article/details/126509848
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号