Open minds leads to freedom

Communicating my thoughts on software development

Configuration classes with Enums

Posted by rnaufal on September 23rd, 2010

As I mentioned on my previous post, an alternative implementation to create Singleton in Java is with Enum types.

Extending the idea, it is interesting to create classes which read configuration values from Properties files with Enum classes. Below is an example:

public enum Configuration {

    HOST("host"),
    PORT("port"),
    MAIL_SERVER("mailServer"),
    INPUT_DIRECTORY("inputDirectory"),
    OUTPUT_DIRECTORY("outputDirectory");

    private static Properties properties;
    static {
        properties = new Properties();
        try {
            properties.load(Configuration.class.getClassLoader().getResourceAsStream(
                "configuration.properties"));
        } catch (Exception e) {
            throw new RuntimeException("Error when loading configuration file", e);
        }
    }

    private String key;

    Configuration(String key) {
        this.key = key;
    }

    public String getKey() {
        return key;
    }

    public String getValue() {
        return properties.getProperty(key);
    }
}

Configuration classes which read values from properties files should be Singletons that are loaded once on the application startup time.

The Configuration values can be used this way:

System.out.println(Configuration.HOST.getValue());

This example shows that the key and value from properties are stored with Enum constants. They are type-safe, they can be easily accessed through code completion in your favourite IDE and they can take advantage of refactoring tools.

Share and Enjoy:
  • Twitter
  • Digg
  • del.icio.us
  • Reddit
  • Facebook
  • Google Bookmarks
  • StumbleUpon
  • Technorati
  • FriendFeed
  • Google Buzz
  • MySpace
  • Slashdot
  • Bruno Patini Furtado

    Nice tip!