Description of the problem
I have a properties file that is accessed throughout the entire java project.
An example of the contents of my .properties file:
appName=MyApp appType=TypeA
Let's say I access the only property appName , all throughout the java project
props.getProperty("appName");
I do not want to iterate over the properties file to get the property value; I just get one property value from the properties file. But I don’t like the fact that I need to access the property using a hard-coded string, because this can lead to maintenance problems (i.e. Change all instances of the hard-coded string).
My current approach
In my current approach, I have a utility class that creates static final variables that represent the key names in a property file, and then I use this variable to access the property value:
public static final String APP_NAME = "appName"; ... props.getProperty(APP_NAME);
But this seems redundant because it is redundant and is still a potential service concern. The key already exists in the properties file, and I declare them again in my utility class.
Is there a more “maintenance free” way to access the key name in my code when using get methods to access property values?
source share