Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

spring

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?

like image 1000
peter.petrov Avatar asked May 13 '14 12:05

peter.petrov


People also ask

How do you use context property placeholder?

The context:property-placeholder tag is used to externalize properties in a separate file. It automatically configures PropertyPlaceholderConfigurer , which replaces the ${} placeholders, which are resolved against a specified properties file (as a Spring resource location).

Where should spring properties be placed?

properties in default location. Spring Boot loads the application. properties file automatically from the project classpath. All you have to do is to create a new file under the src/main/resources directory.

What is Propertysourcesplaceholderconfigurer?

PropertyPlaceHolderConfigurer is a handy way to externalize the properties you want to use in a property file so that they're still resolved when starting the app. by. Gaurav Rai Mazra. May. 16, 17 · Java Zone · Tutorial.

What are properties in spring?

So in a spring boot application, application. properties file is used to write the application-related property into that file. This file contains the different configuration which is required to run the application in a different environment, and each environment will have a different property defined by it.


1 Answers

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.

like image 109
geoand Avatar answered Oct 06 '22 03:10

geoand