yaml的使用

yaml的使用

  • pom的依赖
  • java类与yaml的对应关系
  • 加载yaml文件
  • 具体的代码
    • 取得yaml文件并且转化为java实体类
    • java实体类
      • ServerConfig
      • NodeOptions
      • ExtInterface
  • 之前写的一遍yaml博客

pom的依赖



  org.yaml
  snakeyaml
  1.16

java类与yaml的对应关系

yaml的使用_第1张图片
serverconfig.yaml的存放路径
/resources/localConfig/serverconfig.yaml
在这里插入图片描述

加载yaml文件

yaml的使用_第2张图片

具体的代码

取得yaml文件并且转化为java实体类

public class YamMain {
    public static void main(String[] args) throws IOException {
        File serverconfig = new File(YamMain.class.getResource("/localConfig/serverconfig.yaml").getFile());
        try (
                FileInputStream fileInputStream = new FileInputStream(serverconfig);
                InputStreamReader inputStreamReader=new InputStreamReader(fileInputStream,"UTF-8");
        ){
            Yaml yaml = new Yaml(new org.yaml.snakeyaml.constructor.Constructor(ServerConfig.class));
            ServerConfig serverConfig = yaml.loadAs(inputStreamReader, ServerConfig.class);
            System.out.println(serverConfig.getGroupId());
        }
    }
}

java实体类

ServerConfig

package com.qbsea.yamload1.config;

public class ServerConfig {
    private String groupId;
    private String serverAddress;
    private String raftAddresses;
    private NodeOptions nodeOptions;
    private ExtInterface extInterface;
    //set and get method

    public String getRaftAddresses() {
        return raftAddresses;
    }

    public void setRaftAddresses(String raftAddresses) {
        this.raftAddresses = raftAddresses;
    }

    public String getGroupId() {
        return groupId;
    }
    public void setGroupId(String groupId) {
        this.groupId = groupId;
    }
    public String getServerAddress() {
        return serverAddress;
    }
    public void setServerAddress(String serverAddress) {
        this.serverAddress = serverAddress;
    }
    public NodeOptions getNodeOptions() {
        return nodeOptions;
    }

    public void setNodeOptions(NodeOptions nodeOptions) {
        this.nodeOptions = nodeOptions;
    }
    public ExtInterface getExtInterface() {
        return extInterface;
    }
    public void setExtInterface(ExtInterface extInterface) {
        this.extInterface = extInterface;
    }
}

NodeOptions

package com.qbsea.yamload1.config;

public class NodeOptions {
    private String logUri;
    private String raftMetaUri;
    private String snapshotUri;
    //set and get method
    public String getLogUri() {
        return logUri;
    }

    public void setLogUri(String logUri) {
        this.logUri = logUri;
    }

    public String getRaftMetaUri() {
        return raftMetaUri;
    }

    public void setRaftMetaUri(String raftMetaUri) {
        this.raftMetaUri = raftMetaUri;
    }

    public String getSnapshotUri() {
        return snapshotUri;
    }

    public void setSnapshotUri(String snapshotUri) {
        this.snapshotUri = snapshotUri;
    }
}

ExtInterface

public class ExtInterface {
    private String alarmUrl;
    //set and get method
    public String getAlarmUrl() {
        return alarmUrl;
    }
    public void setAlarmUrl(String alarmUrl) {
        this.alarmUrl = alarmUrl;
    }
}

之前写的一遍yaml博客

你可能感兴趣的:(yaml的使用)