使用SnakeYAML读取yaml配置文件

很多应用都开始使用YAML作为程序配置文件,关于YAML详细介绍可以参考YAML 1.2 (3rd Edition),本文使用SnakeYAML来解析YAML。

maven依赖

<dependency>
    <groupId>org.yamlgroupId>
    <artifactId>snakeyamlartifactId>
    <version>1.17version>
dependency>

示例

sample.yml

version: 1.0
released: 2017-02-14

# Connection parameters
connection:
    driverClass: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/db
    username: root
    password: 12345
    poolSize: 5

# Protocols
protocols:
   - http
   - https

# Developers
developers:
    tom: tom@gmail.com
    bob: bob@gmail.com

对应的JavaBean类

Connection.java

package com.bytebeats.code.yaml.model;

/**
 * ${DESCRIPTION}
 *
 * @author Ricky Fung
 * @date 2017-02-14 15:18
 */
public class Connection {
    private String driverClass;
    private String url;
    private String username;
    private String password;
    private int poolSize;

    public String getDriverClass() {
        return driverClass;
    }

    public void setDriverClass(String driverClass) {
        this.driverClass = driverClass;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public int getPoolSize() {
        return poolSize;
    }

    public void setPoolSize(int poolSize) {
        this.poolSize = poolSize;
    }

}

Configuration.java

package com.bytebeats.code.yaml.model;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * ${DESCRIPTION}
 *
 * @author Ricky Fung
 * @date 2017-02-14 15:18
 */
public class Configuration {
    private Date released;
    private String version;
    private Connection connection;
    private List protocols;
    private Map developers;

    public Date getReleased() {
        return released;
    }

    public void setReleased(Date released) {
        this.released = released;
    }

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    public Connection getConnection() {
        return connection;
    }

    public void setConnection(Connection connection) {
        this.connection = connection;
    }

    public List getProtocols() {
        return protocols;
    }

    public void setProtocols(List protocols) {
        this.protocols = protocols;
    }

    public Map getDevelopers() {
        return developers;
    }

    public void setDevelopers(Map developers) {
        this.developers = developers;
    }

}

解析yaml


Yaml yaml = new Yaml();
try {
    InputStream in = SnakeYAMLTest.class.getResourceAsStream("/sample.yml");
    Configuration config = yaml.loadAs(in, Configuration.class );
    System.out.println(config);
} catch (Exception e){
    e.printStackTrace();
}

序列化

Configuration config = new Configuration();
config.setReleased(new Date());
config.setVersion("1.0");

Connection connection = new Connection();
connection.setDriverClass("com.mysql.jdbc.Driver");
connection.setUrl("jdbc:mysql://localhost:3306/db");
connection.setUsername("root");
connection.setPassword("root");
connection.setPoolSize(10);
config.setConnection(connection);

config.setProtocols(Arrays.asList("http", "https"));
config.setDevelopers(new HashMap());

Yaml yaml = new Yaml();
String output = yaml.dump(config);
System.out.println(output);

源代码

https://github.com/TiFG/daily-codelab/tree/master/yaml-sample

你可能感兴趣的:(Java)