How to Handle Project Configuration

Every web project needs some environment-specific configurations. Database credentials, root url, smtp settings, to name a few. It’s a recurring question on stackoverflow, and I’ve seen a lot of variations on the topic, and here I’ll describe the one that I think is best.

  • put all such properties in a .properties file (for example
  • have that sitting outside the web application. It is not bundled with the build
  • provide a command-line option that indicates where that file is located. For example -Dconfig.location=/home/you/config
  • on application startup (in a ServletContextListener usually) load the file and put it in a map. Can be java.util.Properties or a HashMap
  • for spring users – use <context:property-placeholder-configurer location="file://${config.location}/" . Other frameworks will likely have some mechanism for loading such global properties
  • hold a skeleton properties file in the SCM repository. It should contain all properties, but their values are irrelevant – they will change on each environment
  • the ops team is likely to benefit from versioning different environment configurations (production, qa, stage), so a separate /config folder/subproject can be created and all environment-specific properties can be stored there. When adding a property developers should go and update all files accordingly
  • properties that are not dependent on the environment, but are still global for the project, can be stored within the project (src/main/resources for maven), and committed to SCM. They can be merged with the external properties on startup (merged in memory, that is)
  • most of the externalizable properties can have reasonable defaults – the smtp server of the company, the database driver, etc. They can be placed in a within the project, and the external file can override them. This is just an option – if you are going to have a file committed in the repository with those reasonable defaults, and each environment to use that file as a basis, it’s virtually the same

Developers can easily run their projects that way. Ops can easily deploy builds on different environments. The build remains environment-agnostic.

15 thoughts on “How to Handle Project Configuration”

  1. ‘s/property-placeholder-configurer/property-placeholder/’ – do not confuse with PropertyPlaceholderConfigurer class name. Nice wrap-up BTW.

  2. What do you think are the pros/cons of storing the configuration properties in a database instead of using a properties file?

  3. You have to configure database access somehow, and for that you will need a properties file. Another thing is initialization – you will need database scripts to initialize your database. And what about in-memory database for tests – initialize them as well. Then versioning – operations can use VCS to store properties files, while it will be a bit harder to track that in the DB (it’s not hard, but it’s harder). In short – storing them in DB adds unneeded complexity.

  4. Thanks for article. Nice to see that’s an approach we have set for our project independently on your article 🙂

    With only one difference: we deploy many copies of the same war (content is the same but name and configuration differ) into one Tomcat server. Hence we can’t rely on system properties, instead we have to derive properties file name from war file name. For example: for app1.war there is, for app2.war there is

  5. Well, and how about picking up changes in properties at runtime? This is an important aspect of maintaining live servers, since one cannot afford of having downtime just for the sake of configuration changes. I believe in such cases having config stored in DB is the cleanest solution.

    What do you think?

  6. There are ways to make .properties reloadable. Take log4j/logback for example – if you change logging configuration at runtime, it is reflected without start/stop.

  7. Where should we store a dynamic configuration, changed by Ops through a WebUI ?
    Properties are key-value pairs. Is it simpler to manage in a file, or in a DB ? With a file you can easily save/restore, but you loose transaction.

    As of webapps, i recommend to use the web.xml entry “resource-env-ref”, and obtains the value through JNDI. With tomcat, you simply provide a context.xml with the right config file URL.

  8. Now that cloud based applications are getting more common (Red Hat OpenShift, Java EE 7) it is not a good idea to depend on runtime parameters. The developer has no control over these runtime and environment variables anymore.

    The best way and cleanest solution is to store application specific properties in the database. A database connection has to be configured by a datasource within the application server.

  9. Hi there, While analysing backlinks for some mysterious Google rankings, I found some interesting links originated from your WP blog. It looks like your site has been hacked as it seems pretty unlikely you d normally link out to websites that sell Viagra. Don t worry this happens a lot of the time and there are some simple steps you can take to fix the problem. Read this Secure WordPress for a background on typical vulnerabilities and best practices against hacking

  10. I do agree with all of tthe ideas you have presented for your post.
    They’re very convincing and can defnitely work. Still, the posts are very brief for novices.
    Could you please extend them a biit from next time?

    Thanks for the post.

Leave a Reply

Your email address will not be published.