Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is property resolution order in Spring property placeholder configurer with multiple locations?

Lets say I have a configuration:

    <bean id="batchJobProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>first.properties</value>
            <value>second.properties</value>
        </list>
    </property>
</bean>

first.properties has property "my.url=first.url" second.properties has property "my.url=second.url"

So which value will be injected to the "myUrl" bean? Is there is any defined order of properties resolution?

like image 531
Vladimir Avatar asked Jan 07 '13 08:01

Vladimir


People also ask

What is property placeholder in Spring?

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).

How do you load properties file based on environment in spring boot?

Environment-Specific Properties File. If we need to target different environments, there's a built-in mechanism for that in Boot. We can simply define an application-environment. properties file in the src/main/resources directory, and then set a Spring profile with the same environment name.

What can I use instead of PropertyPlaceholderConfigurer?

Just use: final var configurer = new PropertySourcesPlaceholderConfigurer(); configurer. setProperties(properties); Side note: It even works as a replacement for configuring properties over the XML configuration.

How application properties work in spring boot?

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.


2 Answers

The javadoc for PropertiesLoaderSupport.setLocation states

Set locations of properties files to be loaded.

Can point to classic properties files or to XML files that follow JDK 1.5's properties XML format.

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.

So the value of my.url in second.properties will override the value of my.url in first.properties.

like image 63
Mark Avatar answered Sep 22 '22 14:09

Mark


The last one wins.

Assuming we have props1.properties as

prop1=val1

and props2.properties

prop1=val2

and context.xml

<context:annotation-config />
<bean id="batchJobProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>/props1.properties</value>
            <value>/props2.properties</value>
        </list>
    </property>
</bean>
<bean class="test.Test1" /> 

then

public class Test1 {
    @Value("${prop1}")
    String prop1;

    public static void main(String[] args) throws Exception {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("/test1.xml");
        System.out.println(ctx.getBean(Test1.class).prop1);
    }

}

prints

val2

and if we change context as

        <list>
            <value>/props2.properties</value>
            <value>/props1.properties</value>
        </list>

the same test prints

val1
like image 20
Evgeniy Dorofeev Avatar answered Sep 22 '22 14:09

Evgeniy Dorofeev