如何从一个.yaml文件加载一个配置到一个java类?

8fsztsew  于 2023-05-12  发布在  Java
关注(0)|答案(2)|浏览(168)

我正在使用snakeyaml库加载yaml文件,但它显示一个错误

这是我的Settings类的代码,它有一个表示数据库设置的Database对象

package com.example;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

import org.yaml.snakeyaml.Yaml;

public class Settings {
    private final static InputStream file;
    private final static Settings parsedSettings;
    public Database database;
    static {
        try {
            file = new FileInputStream("./src/main/java/com/example/settings.yaml");
            Yaml yaml = new Yaml();
            parsedSettings = yaml.loadAs(file, Settings.class);
        } catch(FileNotFoundException ex) {
            ex.printStackTrace();
            throw new RuntimeException("Settings file not found or cannot be readed");
        }
    }
    public Settings(){
        this.database = parsedSettings.database;
    }

    public class Database {
        private String driver;
        private String user;
        private String password;
        private String host;
        private int    port;
        private String name;

        private Database(String driver, String user, String password, String host, int port, String name){
            this.driver   = driver;
            this.user     = user;
            this.password = password;
            this.host     = host;
            this.port     = port;
            this.name     = name;
        }

        public String getDriver() {
            return this.driver;
        }
        public String getUser() {
            return this.user;
        }
        public String getPassword() {
            return this.password;
        }
        public String getHost() {
            return this.host;
        }
        public int getPort() {
            return this.port;
        }
        public String getDatabaseName() {
            return this.name;
        }
    }
}

这是我的settings.yaml文件:

database:
  driver: mysql
  user: root
  password:
  host: localhost
  port: 3306
  name: java_messages_app

我没有使用任何框架,我只使用库
我不知道我是否必须在构造函数中加载或以静态方式加载。
对不起我的英语,我使用谷歌翻译

yzuktlbb

yzuktlbb1#

我不建议在static初始化器中加载文件。
这样,snakeyaml创建了一个Settings的示例,该示例由于构造函数中的语句而抛出一个NullPointerException
我的建议是:

public class Settings {
    private final static InputStream file;
    private final static Settings parsedSettings;
    public Database database;
    public Settings() {} //or just remove this

    public static Settings loadFromFile() {
        try {
            file = new FileInputStream("./src/main/java/com/example/settings.yaml");
            Yaml yaml = new Yaml();
            return yaml.loadAs(file, Settings.class);
        } catch(FileNotFoundException ex) {
            ex.printStackTrace();
            throw new RuntimeException("Settings file not found or cannot be readed");
        }
        return null;
    }

    //...rest of class
}

现在只要在需要Settings示例的任何地方调用loadFromFile方法即可。

yc0p9oo0

yc0p9oo02#

你可以使用apache commons configuration

<dependency>
  <groupId>commons-configuration</groupId>
  <artifactId>commons-configuration</artifactId>
  <version>1.6</version>
</dependency>

这必须在您的代码中,您希望读取值的位置:

PropertiesConfiguration config = new PropertiesConfiguration();
config.load(PROPERTIES_FILENAME);
config.getInt("key");

假设你的yml看起来像这样:-

key: value

相关问题