简体   繁体   中英

How does Spring properties look up/precedence work?

I've been fighting with this for some time now. I've google around and tried several stuff but everything I found couldn't solve my problem.

    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations" ref="propertiesLocations" />
        <property name="searchSystemEnvironment" value="true" />
        <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
    </bean>

And this is what I have as my propertiesLocations.

<beans profile="">
    <util:list id="propertiesLocations">
        <value>classpath:com/lala/project/configuration/core.properties
        </value>
        <value>classpath*:com/lala/project/**/configuration/*.properties
        </value>
        <value>classpath*:com/lala/project/**/test/configuration/*.properties
        </value>
        <value>classpath*:project.properties
        </value>
    </util:list>
</beans>

<beans profile="test">
    <util:list id="propertiesLocations">
        <value>classpath:com/lala/project/configuration/core.properties
        </value>
        <value>classpath*:com/lala/project/**/configuration/*.properties
        </value>
        <value>classpath*:com/lala/project/**/test/configuration/*.properties
        </value>
        <value>classpath*:project-test.properties
        </value>
        <value>classpath*:project.properties
        </value>
    </util:list>
</beans>

<beans profile="testing">
    <util:list id="propertiesLocations">
        <value>classpath:com/lala/project/configuration/core.properties
        </value>
        <value>classpath*:com/lala/project/**/configuration/*.properties
        </value> <!-- production properties -->
        <value>classpath*:com/lala/project/**/test/configuration/*.properties <!-- test properties -->
        </value>
        <value>classpath*:project-testing.properties
        </value>
        <value>classpath*:project.properties
        </value>
    </util:list>
</beans>

And then, in one my subprojects, I have 2 properties files, my "production" properties under

src/main/resources/com/lala/project/subproject1/subprojectA/configuration/myProperties.properties

and my "test" properties under

src/test/resources/com/lala/project/subproject1/subprojectA/test/configuration/myProperties.properties

Obviously, these files have pretty much the same properties names, with different values. What I would like to know is why my tests in subprojectA keep picking up my "production" properties instead of my "test" properties? In other words, why does spring doesn't pick up my "test" properties and override my "production properties"?

I forgot to mention that I can't simply erase the "production" properties location for my test profile, as I need the production properties from other projects, subprojects.

I'm just posting my own answer in case anyone stumbles upon a similar problem.

As I had correctly understood, the precedence for properties files locations works downwards. In other words, the location at bottom has/takes the most precedence.

BUT, the problem was not in the lookup precedence but in the way that lookup is made. It appears as if Spring doesn't like these two lines:

    <value>classpath*:com/lala/project/**/configuration/*.properties
    </value>
    <value>classpath*:com/lala/project/**/test/configuration/*.properties
    </value>

What I concluded after much experimenting is that, as the routes/locations matched by the second regexp are also matched by the first one , they are taken into account by the first line and then are not taken into account in the second line (I'm thinking the lookup process as it process line by line from top to bottom).

So what I ended up doing is change the locations of my test properties from something like

    <value>classpath*:com/lala/project/**/test/configuration/*.properties
    </value>

to something like

    <value>classpath*:com/lala/project/**/configuration/test/*.properties
    </value>

尝试更改测试属性路径,如下所示:

file:src/test/resources/com/lala/project/configuration/core.properties

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM