gpt4 book ai didi

java - PropertySources 中各种源的优先级

转载 作者:搜寻专家 更新时间:2023-10-31 20:01:14 27 4
gpt4 key购买 nike

4.0 以来,

Spring 为所有标记为 @Configuration 的类引入了一个新的注解 @PropertySources。它采用不同的 @PropertySource 作为参数。

@PropertySources({
@PropertySource("classpath:application.properties"), @PropertySource("file:/tmp/application.properties")})

我感兴趣的是在多个属性文件中存在的相同键的值发生冲突时的排序。我还没有看到任何与此相关的指定排序的文档。我试了很多次,发现后面提到的 PropertySource 覆盖了前面提到的 PropertySource 中存在的值。但是,如何确定呢?

最佳答案

@PropertySources 的文档没有说明同一属性存在于多个@PropertySource 文件中的情况。

但是,@PropertySource 的文档说明如下:

In cases where a given property key exists in more than one .properties file, the last @PropertySource annotation processed will 'win' and override

由于@PropertySources 中的@PropertySource 声明实际上是一个表,因此可以相当安全地假设最后声明的@PropertySource 会覆盖之前的声明。这与我所做的测试以及此 blog post 一致.

但是,正如问题中提到的,文档中没有明确指出。因此,该行为将来可能会“意外地”改变。

关于java - PropertySources 中各种源的优先级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32251351/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com