简体   繁体   English

Spring属性如何查找/优先级工作?

[英]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. 我在Google周围搜索并尝试了几种方法,但是我发现的所有内容都无法解决我的问题。

    <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. 这就是我的propertyLocations。

<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 然后,在我的一个子项目中,我有2个属性文件,我的“生产”属性在

src/main/resources/com/lala/project/subproject1/subprojectA/configuration/myProperties.properties 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 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? 我想知道的是为什么子项目A中的测试为什么会继续使用“生产”属性而不是“测试”属性? In other words, why does spring doesn't pick up my "test" properties and override my "production properties"? 换句话说,为什么spring不接受我的“测试”属性并覆盖我的“生产属性”?

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: 似乎Spring不喜欢这两行:

    <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). 经过大量实验后,我得出的结论是,由于第二个正则表达式匹配的路由/位置也被第一个正则表达式匹配 ,因此第一行将它们考虑在内,然后在第二行中不考虑它们(I'我认为查找过程是从上到下逐行处理的)。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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