Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make Java Properties available across classes?

I chose to take properties file for customization of some settings. I use the following code to make a Properties Object available in a class

Properties defaultProps = new Properties();
    try {
        FileInputStream in = new FileInputStream("custom.properties");
        defaultProps.load(in);
        in.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

Do I have to add this to every class? Probably not because then every class would open a stream to this file. But I'm not sure how to handle this properly. Should I make a class MyProperties and instantiate it in whatever class needs properties?

Thanks in advance!

like image 801
tzippy Avatar asked Jan 20 '11 16:01

tzippy


People also ask

Do Java classes have properties?

Class Properties. The Properties class represents a persistent set of properties. The Properties can be saved to a stream or loaded from a stream. Each key and its corresponding value in the property list is a string.

Which class is used to access the values in properties file directly?

load() method of Properties class is convenient to load . properties file in the form of key-value pairs.

Where are Java properties stored?

Normally, the default properties are stored in a file on disk along with the . class and other resource files for the application. Next, the application creates another Properties object and loads the properties that were saved from the last time the application was run.


2 Answers

Once you initialized defaultProps, you can make its contents available to other objects in your app e.g. via a public static accessor method, e.g.:

public class Config {
  private static Properties defaultProps = new Properties();
  static {
    try {
        FileInputStream in = new FileInputStream("custom.properties");
        defaultProps.load(in);
        in.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
  }
  public static String getProperty(String key) {
    return defaultProps.getProperty(key);
  }
}

This is the simplest approach, however it creates an extra dependency which makes unit testing harder (unless you provide a method in Config to set a mock property object for unit testing).

An alternative is to inject defaultProps (or individual configuration values from it) into each object which needs it. However, this may mean you need to add extra parameter(s) to lots of methods if your call hierarchies are deep.

like image 123
Péter Török Avatar answered Oct 13 '22 20:10

Péter Török


If you only need one instance of your properties class you can use the singleton (anti?)-pattern.

It would look like a class like this:

public class MyProperties extends Properties {
    private static MyProperties instance = null;

    private MyProperties() {
    }

    public static MyProperties getInstance() {
        if (instance == null) {
            try {
                instance = new MyProperties();
                FileInputStream in = new FileInputStream("custom.properties");
                instance.load(in);
                in.close();
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
        return instance;
    }
}
like image 21
SirDarius Avatar answered Oct 13 '22 18:10

SirDarius