java Properties - to expose or not to expose?

Posted by ring bearer on Stack Overflow See other posts from Stack Overflow or by ring bearer
Published on 2010-03-04T16:33:27Z Indexed on 2010/04/16 21:33 UTC
Read the original article Hit count: 374

Filed under:
|
|

This might be an age old problem and I am sure everyone has their own ways. Suppose I have some properties defined such as

secret.user.id=user
secret.password=password
website.url=http://stackoverflow.com

Suppose I have 100 different classes and places where I need to use these properties. Which one is good (1) I create a Util class that will load all properties and serve them using a key constant Such as : Util is a singleton that loads all properties and keeps up on getInstance() call.

Util myUtil = Util.getInstance();
String user = myUtil.getConfigByKey(Constants.SECRET_USER_ID);
String password = myUtil.getConfigByKey(Constants.SECRET_PASSWORD);
..
//getConfigByKey() - inturns invokes properties.get(..)
doSomething(user, password)

So wherever I need these properties, I can do steps above.

(2) I create a meaningful Class to represent these properties; say, ApplicationConfig and provide getters to get specific properties. So above code may look like:

ApplicationConfig config = ApplicationConfig.getInstance();
doSomething(config.getSecretUserId(), config.getPassword());
//ApplicationConfig would have instance variables that are initialized during
// getInstance() after loading from properties file.

Note: The properties file as such will have only minor changes in the future.

My personal choice is (2) - let me hear some comments?

© Stack Overflow or respective owner

Related posts about java

Related posts about properties