Over a million developers have joined DZone.

Using More Than One Property File in Spring MVC

· Java Zone

Navigate the Maze of the End-User Experience and pick up this APM Essential guide, brought to you in partnership with CA Technologies

Spring is one of the most important, powerful and also complex frameworks in the actual Java panorama.

Unfortunately for me, I’m very forgetful so it is worth to write a post-reminder to increment the possibilities I never forget again something like this.

Using property files in Spring

Since Spring 3, the use of properties in our code was made really simple thanks to the placeholders.

Supposing a property file my_config_1.properties with next content:

prop1=Some property with some value
prop2=Some number here

It is enough to add the next configuration element in our servlet configuration XML file:


Later, at any place of our program we can easily get the properties using @Valueannotation:

@Value("${prop1}")privateString myProperty;

And what if you have more than one property files?

Supposing we have a second property file my_config_2.properties with the values:

second_file_prop=This comes from the second file

Spring allows to specify more than one properties file adding a new placeholder:


The problem comes when you try to use the property from the second file, using@Value("${second_file_prop}"), Spring will throw an exception saying it can find thesecond_file_prop value. This is because Spring try to find the property in the first file and if it does not find it throws an error.

To avoid this ugly effect we need to use the ignore-unresolvable attribute in the placeholder:


This way if Spring does not find a property in the first file it will continue looking at the second one.

The order

In addition, the placeholder element can use the order attribute that determines the order in which Spring must look at the files. So a good way to express the previous configuration is:


Thrive in the application economy with an APM model that is strategic. Be E.P.I.C. with CA APM.  Brought to you in partnership with CA Technologies.


Published at DZone with permission of Antonio Santiago, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}