为什么不使用ResourceBundle而不是Properties? [英] Why not use ResourceBundle instead of Properties?

查看:178
本文介绍了为什么不使用ResourceBundle而不是Properties?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个我无法找到结论答案的简单问题。

This is an easy question to which I can't find a concluding answer.

我可以加载字符串属性(例如:查询预备语句)一个 config.properties 文件。假设我想要连接到的数据库连接。

I can load string properties (e.g.: a query for a prepared statement) from a config.properties file. Let's say I want to take the database connection to which to connect.

如果我想从文件中获取此信息,我可以在类中执行以下操作:

If I want to take this information from the file, I could do just the following in a class:

    private static final ResourceBundle BUNDLE = ResourceBundle.getBundle("scheduler");
    private static final String DRIVER  = BUNDLE.getString("bd.driver");
    private static final String CONNECTIONURL  =BUNDLE.getString("bd.url");
....

但我见过很多人推荐使用而不是属性,然后我将不得不做同样的事情(如果我想保持类静态,没有一个合适的构造函数):

But instead I've seen that many people recommend using instead Properties, Then I would have to do the same with something like this (if I want to keep the class static and not have a proper constructor):

static {
        prop = new Properties(); 
        try {                prop.load(ReportsDB.class.getClassLoader().getResourceAsStream("config.properties"));
        } catch (IOException ex) {
            Logger.getLogger(ReportsDB.class.getName()).log(Level.SEVERE, null, ex);
            throw new RuntimeException(ex);
        }
    }

    private static final String DRIVER  = prop.getProperty("bd.driver");
    private static final String CONNECTIONURL  = prop.getProperty("bd.url");

那么,为什么我不能使用 ResourceBundle 而不是属性当第二个更详细?

So, why shouldn’t I use the ResourceBundle instead of Properties when the second one is more verbose?

推荐答案


那么,为什么我不应该使用ResourceBundle而不是属性,当第二个是更详细?

So, why shouldn’t I use the ResourceBundle instead of Properties when the second one is more verbose?

因为那不是 ResourceBundle 所针对的。 课程描述以:

Because that's not what ResourceBundle is for. The description of the class starts with:


资源包包含特定于语言环境的对象。当您的程序需要特定于语言环境的资源(例如String)时,您的程序可以从适合当前用户的语言环境的资源包中加载它。通过这种方式,您可以编写程序代码,该程序代码在很大程度上独立于用户的语言环境,隔离资源包中大多数(如果不是全部)特定于语言环境的信息。

Resource bundles contain locale-specific objects. When your program needs a locale-specific resource, a String for example, your program can load it from the resource bundle that is appropriate for the current user's locale. In this way, you can write program code that is largely independent of the user's locale isolating most, if not all, of the locale-specific information in resource bundles.

这听起来像你的用例吗?我不这么认为。

Does any of this sound like your use case? I don't think so.

听起来问题是纯粹加载属性文件的详细程度:所以写一个实用工具方法去做。然后您的代码可以简单:

It sounds like the problem is purely the verbosity of loading a properties file: so write a utility method to do that. Then your code can be simply:

private static final Properties CONFIGURATION = PropertyUtil.load("scheduler.properties");
private static final String DRIVER = CONFIGURATION.getString("bd.driver");
private static final String CONNECTIONURL = CONFIGURATION.getString("bd.url");

不可否认,我并不热衷于以依赖于顺序的方式使用静态字段初始化程序。我很想把所有的配置封装在一个单独的类中,所以你可以这样写:

Admittedly I'm not keen on having static field initializers in an order-dependent way like that... I'd be tempted to encapsulate all of the configuration in a separate class, so you could write:

private static final SchedulerConfiguration CONFIG = 
    SchedulerConfiguration.load("scheduler.properties");

然后使用 CONFIG.getDriver()等可以每次从属性中获取,或使用字段,或其他任何内容。

then use CONFIG.getDriver() etc which could fetch from the properties each time, or use a field, or whatever.

这篇关于为什么不使用ResourceBundle而不是Properties?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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