Working with .properties config files in Java

Managing properties for an enterprise application can be a bit trickier than one might expect.

Q: How would you go about managing properties files in your application?
A: One great advantage of property files is that they let you change your application’s configuration without recompilation. However, you most likely need to restart your application for the new configuration to take effect. However, you most likely need to restart your application for the new configuration to take effect. There are different types of properies files like

1. Environment independent files are archived within a war or ear. These can be loaded via the classpath.

2. Environment independent files stored outside a war or ear. These need to be loaded via a file loader. Define a JVM argument or system property for the path of the file.
3. Environment specific files stored outside a war or ear. These need to be loaded via a file loader. Define a JVM argument or system property for the path of the file.

It is a best practice to store environment specific (e.g. test, dev, uat, staging, prod, etc)  files outside the war or ear archives as the same archive can be deployed to any environment without having to package environment specific archives.

4. Properties file with sensitive information. Store the property values encrypted (Triple DES) or encoded (base64) with a salt. A salt is a secret that can be stored in a database or a separate file with proper access control.

5. Dynamic information in properties file or loading a property file at runtime. For example, in your .properties file you can use

Then use the MessageFormat class in Java

You can also dynamically load .properties file with the load and store methods in java.util.Properties. It is recommended to use an existing configuration library like Apache Commons Configuration to load properties file at runtime without having to bring the server or application down. The commons config does support variable interpolation.

Finally you can use JMX (Java Management eXtension) to write your own managed bean to change proprty values at runtime. A typical real life example would be to change log4j.xml debug levels. Appropriate log levels are very handy in diagnosing problems in production, but log levels like trace or debug can adversely impact performance. Hence, it is very useful to be able to set log levels at runtime.

Here is a sample .properties file that can be used with Apache’s commons configuration.

Q. How would you load a properties file from a classpath or a file system?
A. Here is a very basic code snippet.

Spring has the ability to load properties files as part of the application configuration, for use internally. The properties can be referenced within spring configuration files using ant-style placeholders, e.g. ${app.var}. Here is the link to step by step tutorial.

Spring tutorial: read from .properties files


Java & Big Data Interview FAQs

Java Key Areas Interview Q&As

800+ Java Interview Q&As

Java & Big Data Tutorials

Top