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

I have a project that consists of many applications.
I have it in one of my applications.

<context:property-placeholder location="file:/config/p1.properties,file:/config/p2.properties" />

Now I want to define a property in p2.propertiesthat already exists in p1.properties. That is, I want to basically override this property in p2 (give it a different value). This is because at runtime p1 is shared by many applications, and p2 is used only by my application. Therefore, I do not want to influence all applications, but just my application.

1) I wonder if the value of the property that I will define in p2 will be priority.
2) Does the order in location, and if so, then the second - priority over the first?

+4
source share
3 answers

As you configured property-placeholder, any property that you have in p2.propertieswill take precedence over tags in p1.properties.

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

You have a setting - this is the standard way that SysAdmins or DevOps users override the properties of your application. For example, you may have the first file, which is a classpath property file, and the second, similar to it, a file system-based property file whose values ​​override the values ​​in the first.

JavaDoc PropertiesLoaderSupport ( , PropertySourcesPlaceholderConfigurer) , setLocations

. , , , . , , .

+6

, 2 "order". , .

<!-- 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" />
+8

In my experience, order in location is important. Files are loaded so that they are declared (p1 to p2).
If you declare a property multiple times, this last found value is accepted. (So ​​the value in p2)

We often use this system to override environment properties.

+3
source

Source: https://habr.com/ru/post/1540369/


All Articles