Spring - "context:property-placeholder" - property priority

java, spring

Solution

The way you have configured the `property-placeholder`, any property you have in `p2.properties` will take precedence over the ones in `p1.properties`.

That is because the properties in last file always take precedence.

What you have setup is a standard way for SysAdmins or DevOps people to override properties of your application. You could for example have the first file be a classpath properties file while the second one could be like you have it, a file systems based properties file, whose values override the ones in the first.

If you check the JavaDoc of `PropertiesLoaderSupport` (which handles the loading of the resources and is an abstract class that is extended by `PropertySourcesPlaceholderConfigurer`) you will see that in the `setLocations` method it has the following comment

Note: Properties defined in later files will override properties defined earlier files, in case of overlapping keys. Hence, make sure that the most specific files are the last ones in the given list of locations.

Problem

I have a project which consists of many apps. I have this in one of my apps. `<context:property-placeholder location="file:/config/p1.properties,file:/config/p2.properties" />` Now, I want to define a property in `p2.properties` which already exists in `p1.properties`. That is I want to basically override that property in p2 (give it a different value). This is because at runtime p1 is shared between many apps, and p2 is just used by my app. So I don't want to affect all apps, but just my app. 1) I wonder if the property value which I will define in p2 will take priority. 2) Does the order in `location` matter and if so, does the second one take priority over the first one?

Original source