gpt4 book ai didi

java - 外部化属性工作但不覆盖内部属性中的现有默认值

转载 作者:行者123 更新时间:2023-11-28 23:22:17 26 4
gpt4 key购买 nike

我有一个多模块 maven 项目,每个模块都是一个 jar,具有自己的上下文文件和一组捆绑在 JAR 中的默认属性。

基本上在核心模块中我有这个:

<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:core.properties</value>
</list>
</property>
<property name="ignoreUnresolvablePlaceholders" value="true" />
</bean>

现在我有了另一个模块,它通过加载所有相关的 XML 并添加自己的配置文件来构建应用程序

<import resource="classpath*:core-context.xml" />


<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:application.properties</value>
<value>#{systemEnvironment['foo_bar']}</value>
</list>
</property>
<property name="ignoreUnresolvablePlaceholders" value="true" />
</bean>

发生的事情是我的所有配置文件都工作正常,除了具有默认值的那个。我无法在 application.properties 或系统变量指向的文件中覆盖它们。

我尝试使用 spring.config.location 强制配置文件。我确实在 tomcat 中看到消息说我有配置 -Dspring.config.file=file:///... (windows 路径)。但是 spring 完全忽略了它(Spring 的日志中没有消息)。

我尝试切换到 PropertySources 类,但没有成功。

我非常希望不必删除所有默认属性并将所有内容放在外部文件中,因为很多参数都是应用程序内部的,对客户端没有任何值(value)。

那么我在这里缺少什么?

最佳答案

好吧,经过几个小时后,我终于发现了一些非常……简单的事情。技巧如下:在导入之前声明属性占位符。

当您在属性占位符中声明文件时,它是最后一个获胜,这里似乎恰恰相反:第一个占位符获胜。

    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:application.properties</value>
<value>#{systemEnvironment['foo_bar']}</value>
</list>
</property>
<property name="ignoreUnresolvablePlaceholders" value="true" />
</bean>

<import resource="classpath*:core-context.xml" />

关于java - 外部化属性工作但不覆盖内部属性中的现有默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41977892/

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