Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why not use ResourceBundle instead of Properties?

Tags:

java

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

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");

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

like image 632
Christian Vielma Avatar asked Feb 14 '13 19:02

Christian Vielma


People also ask

What is a ResourceBundle?

A resource bundle is a set of properties files with the same base name and a language-specific suffix. For example, if you create file_en. properties and file_de. properties, IntelliJ IDEA will recognize and combine them into a resource bundle.

What is the ResourceBundle class?

ResourceBundle class is used to store text and objects which are locale sensitive. Generally we use property files to store locale specific text and then represent them using ResourceBundle object. Following are the steps to use locale specific properties file in a java based application.

How do I use ResourceBundle getBundle?

getBundle. Gets a resource bundle using the specified base name, locale, and class loader. This method behaves the same as calling getBundle(String, Locale, ClassLoader, Control) passing a default instance of ResourceBundle.


1 Answers

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

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

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");

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

like image 110
Jon Skeet Avatar answered Oct 25 '22 02:10

Jon Skeet