springboot2.0 读取yml 配置 array,list,map,单值,及其组合

文章目录


1. yml key特殊符号处理

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

app:
  "[/api/**]": anno

2. yml @Value 属性配置

基本类型

spring:
  profiles:
    active: dev

typechange:
  turnOn: 1

java

/**
 * 部署环境
 */
@Value("${spring.profiles.active:prod}")
private String env;

/**
 * 1:开启 0:关闭
 */
@Value("${typechange.turnOn}")
private Integer turnOn;

数组 Array

test:
  array1: aaa,bbb,ccc
  array2: 111,222,333
  array3: 11.1,22.2,33.3

java

// : 冒号后的值表示当 key 不存在时候使用的默认值,使用默认值时数组的 length = 0

@Value("${test.array1:}")
private String[] testArray1;

@Value("${test.array2:}")
private int[] testArray2;

@Value("${test.array3:}")
private double[] testArray3;

List

test:
  list: aaa,bbb,ccc

java

// yml必须要有此配置,不然启动会报错
@Value("#{'${test.list}'.split(',')}")
private List<String> testList;

//加默认值并判断集合非空,避免不配置这个 key 时候程序报错
@Value("#{'${test.list:}'.empty ? null : '${test.list:}'.split(',')}")
private List<String> testList;

Set 去重

test:
  set: 111,222,333,111

java

@Value("#{'${test.set:}'.empty ? null : '${test.set:}'.split(',')}")
private Set<Integer> testSet;

// output: [111, 222, 333]

Map

yml配置

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

Java

@Value("#{${test.map1}}")
private Map<String,String> map1;

@Value("#{${test.map2}}")
private Map<String,Integer> map2;

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

public class MapDecoder {
    public static Map<String, String> decodeMap(String value) {
        try {
            return JSONObject.parseObject(value, new TypeReference<Map<String, String>>(){});
        } catch (Exception e) {
            return null;
        }
    }
}

java使用

@Value("#{T(com.github.jitwxs.demo.MapDecoder).decodeMap('${test.map1:}')}")
private Map<String, String> map1;

@Value("#{T(com.github.jitwxs.demo.MapDecoder).decodeMap('${test.map2:}')}")
private Map<String, String> map2;

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

java用法:

@Component
@ConfigurationProperties(prefix = "person")
@Data
public class Person {
 
    private String lastName;
    
    private Integer age;
    
    private Boolean boss;
    
    private Date birth;
 
    private Map<String, String> maps;
    
    private List<String> lists;
    
    private Dog dog;
    
    private Map<String,List<String>> varmaplist;

}    

Map<String, Entity> 数据结构配置

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

java 用法:

/**
 * 系统公共配置
 *
 * @author zhaoyang10
 * @date 2020/7/16
 */
@RefreshScope
@Component
@ConfigurationProperties(prefix = "systems")
@Data
public class NacosSystemConfig {

    private Map<String, SystemInfo> 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;
}

4. 参考文档

spring boot 读取配置文件

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

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

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

springboot yml配置list、map组合写法

Spring boot2:Profile使用

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

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值