Question

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?

Was it helpful?

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.

OTHER TIPS

You can be explicit about the order of precedence by defining 2 placeholders and using the 'order' property. Below is an example from one of my apps where an external config file will always override the one on the classpath.

<!-- DEFINE TWO CONFIGURATION FILES: FOR LOCAL DEVELOPMENT THESE WILL NORMALLY 
        BE READ FROM A FILE BUNDLED WITH APPLICATION. ONCE DEPLOYED THESE SHOULD 
        BE READ FROM AN EXTERNAL CONFIGURATION FILE AT THE SPECIFIED LOCATION. THE 
        EXTERNAL FILE SHOULD OVERRIDE THE BUNDLED FILE (CONTROLLED VIA THE ORDER 
        ATTRIBUTE: LOWEST WINS) -->

    <context:property-placeholder
        location="file:/apps/jboss-jpp/jboss-jpp-6.0/standalone/configuration/land.properties"
        ignore-resource-not-found="true" ignore-unresolvable="true" order="1" />

    <context:property-placeholder location="classpath*:/land.properties"
        ignore-resource-not-found="false" ignore-unresolvable="false" order="2" />

In my experience, the order in location is important. The files are loaded in order they are declared (p1 before p2).
If you declare a property multiple times, it is the last found value that is taken. (So the value in p2)

We use this system often to override environment specific properties.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top