首页 > 技术文章 > Yaml spring boot 二维数组写法

leohe 2018-02-01 11:44 原文

方案一:

Yaml

channel:
  info:
    - channel-ip: 192.168.1.40
      channel-no: 5182001001
    - channel-ip: 192.168.1.10
      channel-no: 5182000002
    - channel-ip: 192.168.1.30
      channel-no: 5182001003
    - channel-ip: 192.168.1.20
      channel-no: 5182000004

JavaBean: 分2个, 不要用内部类的形式

@Component
@Order(value = 1)
@ConfigurationProperties(prefix = "channel")
@JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class)
public class ChannelProperties {

    private List<ChannelInfoPreperties> info = new ArrayList<>();

    public List<ChannelInfoPreperties> getInfo() {
        return info;
    }

    public void setInfo(List<ChannelInfoPreperties> info) {
        this.info = info;
    }

}

 

@Component
@Order(value = 1)
@ConfigurationProperties(prefix = "channel.info")
@JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class)
public class ChannelInfoPreperties {

    private String channelIp;
    private String channelNo;

    public String getChannelIp() {
        return channelIp;
    }

    public void setChannelIp(String channelIp) {
        this.channelIp = channelIp;
    }

    public String getChannelNo() {
        return channelNo;
    }

    public void setChannelNo(String channelNo) {
        this.channelNo = channelNo;
    }
}

  

方案二:

application:
  gates:
    - name: A
      lanes:
        - A01
        - A03
    - name: B
      lanes:
        - B01
        - B02

 

@Component
@ConfigurationProperties(prefix = "application")
@Setter
@Getter
@Slf4j
public class GateInfoConfig {
    private List<GateInfo> gates = new ArrayList<>();

    @Setter
    @Getter
    @ToString
    public static class GateInfo {

        private String name;
        private String[] lanes;
    }
}

此方案中yml的名字需要与bean的属性名一致,如例子中的 gates、name、lanes

推荐阅读