在java中管理配置(初始配置/保存/加载配置) [英] Managing configurations in java (initial config / save/load config)

查看:134
本文介绍了在java中管理配置(初始配置/保存/加载配置)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类似于Config的类:

I got a class Config wich looks like that:

public Class Config {

    public static int someIntValue = 0;
    public static String someText = "some text";

}

我现在要做的是保存并加载该配置如果不需要加载另一个,也有初始配置。所以配置可以在程序中的任何一点改变。

What i want to do now is saving and loading that config and there is also that inital config if there is no need to load another. So the config can change at any point in the programm.

我想出的是像Singelton一样的模式

What i came up with was a Singelton like pattern

public Class Config {

    public static Config instance;

    private int someIntValue = 0;
    private int String someText = "some text";

    public static Config getInstance(){
    if(instance == null)
    instance = new Config();
    return instance;
    }

    public void setInstance(Config config){
    this.instance = config;
    }
    //getter/setter
    ...

}

但最终它看起来不是最好的方法而且对它不太满意:/
也许你们可以用通常的最佳实践方式帮助我这样做。

But in the end it doesnt look like the best approach and im not quite happy with it :/ Maybe you guys can help me out with a usual / "best practice" way to do that.

最好的问候
制造

Best Regards Made

推荐答案

我只是使用java.util.Properties,或者它周围的一些包装器。另一个好的方法是java bean,比如xstream来保存/加载东西。

I would just use java.util.Properties, or some wrapper around it. Another good approach is java bean and something like xstream to save/load stuff.

这篇关于在java中管理配置(初始配置/保存/加载配置)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆