0

To externalize a .properties file, currently I am keeping it in a file system say C:\test\UI\properties and I have created a Environment variable $PROP_LOCATION which points to above file location. So whenever I need to change properties, just I go to that location edit properties and refresh the application. It works like a charm.

So is this the best way? Or is there any other way you professionals suggest me to keep properties file out of war file?

Note: The above thing works fine in both unix and windows environment as I have a "if" condition, if one of the environment variable not found I will check for another environment variable.

Pradeep Simha
  • 16,115
  • 16
  • 51
  • 103
  • 2
    possible duplicate of [Where to place configuration properties files in a JSP/Servlet web application?](http://stackoverflow.com/questions/2161054/where-to-place-configuration-properties-files-in-a-jsp-servlet-web-application) – BalusC Nov 21 '12 at 13:33

3 Answers3

1

I usually reference external properties files by adding the directory to the classpath.

For example, if example.properties is in path C:\test\UI\properties directory, I call JVM as follows:

java -classpath C:\test\UI\properties MyJavaProgram

Inside the Java code, you can load the properties via the following mechanism:

Properties p = new Properties();
p.load(getClass().getClassLoader().getResourceAsStream("/example.properties"));
Jin Kim
  • 13,866
  • 16
  • 51
  • 79
1

I agree that just packaging the properties file with your application makes the most sense. If you have some valid reason not to do that, your solution seems fine as long as in deployment the file is placed in a location that your application has access to and is not exposed to the Internet.

I am doing something somewhat analagous to you. In an application I'm running, there is a page of announcements and other messages that I want users to see, but to make it easier for me and others to edit these messages, they are loaded from another place on the server, not even in the Tomcat folder. I can then easily edit the messages without even re-deploying.

Thorn
  • 3,935
  • 4
  • 20
  • 42
0

I suggest to use the ReloadableResourceBundleMessageSource of spring

Check it out: http://static.springsource.org/spring/docs/1.2.9/api/org/springframework/context/support/ReloadableResourceBundleMessageSource.html

And as for placing the properties files, I don't think keeping them outside application is good idea. You should package them in war anyway, as they are logically part of your application.

And since you are reloading the application, you can use maven task to build, deploy and reload the app on server everytime the properties change.

Pawel Solarski
  • 978
  • 7
  • 7