Thursday, August 8, 2013

How to dynamically load file properties based on environments: Spring Profiles tutorial

If you are working with different environments which need different property file configurations, maybe the best way is to load a specific profile for each different environment wich load the specific properties for that environment. There are different ways to create profiles in Java, but in this post I will just talk about the way I used.
My applications normally uses Spring framework and Maven for the build/deploy process. In Maven 3 is possible to create profiles but I find it not very usefull because if you want to create application properties dynamically then you need to use ant plugins, so I found another way: using Spring Profiles (avaliable from Spring 3.1).
First of all you need to add the following dependency: Here goes the configuration of the properties that are loaded during the deploy: To retrive a property in your Object just do the following: To make this work you just make sure to set spring.profiles.active variable in your environment somewhere, or add the following parameter in tomcat -Dspring.profiles.active=""

No comments: