I’m developing a web application using Java EE6 and JSF 2.0. I have a .properties file in which I am storing a few constants, which I need to change easily before deploying the app. I know how to read a properties file, but the question is when?
This is the class I’m using to access the properties called CLIENT_ID and CLIENT_SECRET:
public class FConnectProperties {
Properties properties;
public FConnectProperties() throws FileNotFoundException, IOException{
properties = new Properties();
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream stream = classLoader.getResourceAsStream("/FConnect/FConnect.properties");
properties.load(stream);
stream.close();
}
public String getClientID(){
return properties.getProperty("CLIENT_ID");
}
public String getClientSecret(){
return properties.getProperty("CLIENT_SECRET");
}
Since this class reads the properties file from disk, is it ok to instantiate it every time I need to access them? Or should I instantiate it once during application startup, (possibly from a Singleton Bean) and access it from there? What is the best way to go about doing this?
The properties don’t change once the application is launched. I’m only reading them, never updating them.
I would suggest reading the file once at startup, and as you say holding them in a singleton. Since I’m guessing your properties are a selection of key/value pairs, that aren’t all that large, I’d say they should do well in a map-type data structure.
Depending on how often you need to access them, then reading them from disk could become costly.