Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring属性查找/优先级是如何工作的?_Java_Spring_Spring Profiles_Spring Properties - Fatal编程技术网

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

Java Spring属性查找/优先级是如何工作的?,java,spring,spring-profiles,spring-properties,Java,Spring,Spring Profiles,Spring Properties,我已经为此奋斗了一段时间了。我在谷歌上搜索了很多东西,但我发现的一切都不能解决我的问题 <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations" ref="propertiesLocations" /> <property name="searchSystemE

我已经为此奋斗了一段时间了。我在谷歌上搜索了很多东西,但我发现的一切都不能解决我的问题

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

这就是我的财产所在地

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

类路径:com/lala/project/configuration/core.properties
classpath*:com/lala/project/**/configuration/*.properties
classpath*:com/lala/project/**/test/configuration/*.properties
classpath*:project.properties
类路径:com/lala/project/configuration/core.properties
classpath*:com/lala/project/**/configuration/*.properties
classpath*:com/lala/project/**/test/configuration/*.properties
classpath*:project-test.properties
classpath*:project.properties
类路径:com/lala/project/configuration/core.properties
classpath*:com/lala/project/**/configuration/*.properties
classpath*:com/lala/project/**/test/configuration/*.properties
classpath*:project-testing.properties
classpath*:project.properties
然后,在我的一个子项目中,我有两个属性文件,我的“生产”属性在下面

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

以及我的“测试”属性

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

显然,这些文件的属性名称几乎相同,但值不同。 我想知道的是,为什么子项目A中的测试总是选择我的“生产”属性,而不是我的“测试”属性?换句话说,为什么spring不选择我的“测试”属性并覆盖我的“生产属性”


我忘了提到我不能简单地删除测试配置文件的“生产”属性位置,因为我需要其他项目、子项目的生产属性。

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

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

我只是发布我自己的答案,以防有人碰到类似的问题

正如我正确理解的,属性文件位置的优先级向下。换句话说,底部的位置具有/优先权

但是,问题不在于查找优先级,而在于查找的方式。Spring似乎不喜欢这两行:

classpath*:com/lala/project/**/configuration/*.properties
classpath*:com/lala/project/**/test/configuration/*.properties
经过大量实验,我得出的结论是,由于第二个regexp匹配的路由/位置也与第一个匹配,因此第一行考虑了它们,第二行则不考虑它们(我认为查找过程是从上到下逐行处理的)

所以我最终做的是改变测试属性的位置

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

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

我尝试过,但没有成功:(.这不是一个完美的解决方案,但我认为至少它应该可以工作……这就是我认为我不完全理解spring属性如何工作的原因。您在哪里加载属性文件?您有两种不同的应用程序上下文(一种用于测试,另一种是真实的应用程序上下文),我认为您总是加载相同的应用程序上下文。请向我们显示您的目录结构。