springboot 在application.yml中定义数组的两种方式

1、通过 ‘-‘ 来区别数组的多个值

yml结构配置:

provinceConf:
      smpIp:
        - 192.168.2.164
        - 192.168.2.166

配置类取数组值:

import lombok.Data;
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.stereotype.Component;
     
    /**
     * @description
     * @create: 
     **/
    @Data
    @Component
    @ConfigurationProperties(prefix = "provinceconf")
    public class YmlListValueConfig {
        private String [] smpIp;//注意smpIp要与配置文件中的smpIp一样
    }

2、通过 逗号 ‘ ,‘ 来区别数组的多个值

yml结构配置:

test:
  strArray: aaa,bbb,ccc
  strs: a,b,c

配置类取数组值:

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

/**
 * @description
 * @author: 
 * @create: 
 **/
@Configuration
public class YmlProperties {

    //获取字符串数组数据
    @Value("${test.strArray}")
    public String[] strArray;

    //获取list数据
    @Value("${test.strArray}")
    public List<String> strList;

    //获取字符串数据
    @Value("${test.strs}")
    public String strs;
}
经验分享 程序员 微信小程序 职场和发展