gpt4 book ai didi

java - 如何在 Spring 中加载系统属性以获取属性文件位置

转载 作者:行者123 更新时间:2023-11-30 10:35:48 26 4
gpt4 key购买 nike

我正在开发一个 Spring 应用程序,其中的属性文件将打包在 .war 文件中以供部署。

<context:property-placeholder location="classpath:application.properties" />

但是,我希望能够用另一个可以在 standalone.xml 中指定为系统属性的文件覆盖它们:

</extensions>

<system-properties>
<property name="CONFIG_FILE_LOCATION" value="/path/to/application.properties"/>
</system-properties>

这是我的解决方案,

<context:property-placeholder location="classpath:application.properties,
file:///${CONFIG_FILE_LOCATION}" />

但显然 Spring 无法找到它

Caused by: java.io.FileNotFoundException: ${CONFIG_FILE_LOCATION} (The system cannot find the file specified)

有人知道我该如何解决吗? Spring 是否有另一种访问系统属性的方式?

最佳答案

Spring 中实际上可以通过使用此解决方案将 system property 位置指定到另一个文件来覆盖某些属性:

<context:property-placeholder location="classpath:alarm_notification.properties, file:///${CONFIG_FILE_LOCATION}" />

如果位于 CONFIG_FILE_LOCATION 的文件中的某个属性未被覆盖,则将使用 application.properties 中的值。

只需确保在用于启动服务器的standalone.bat 文件中具有以下配置:

</extensions>

<system-properties>
<property name="CONFIG_FILE_LOCATION" value="/path/to/application.properties"/>
</system-properties>

关于java - 如何在 Spring 中加载系统属性以获取属性文件位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40930004/

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