gpt4 book ai didi

java - 如何为文件系统上的文件使用属性占位符

转载 作者:IT老高 更新时间:2023-10-28 13:52:47 25 4
gpt4 key购买 nike

我们曾经有一种方法可以从类路径上的文件中加载属性:

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

而且效果很好。但是现在我们想从系统上不在类路径中的特定文件加载属性。我们希望能够动态加载文件,因此我们使用 Java 环境变量来填充它。下面我举一个简单的例子:

在 Java 中:

  System.setProperty("my.prop.file", "/path/to/myConfigFile.properties");

在 Spring XML 中:

<context:property-placeholder location="${my.prop.file}" />

感谢 Luciano 的想法,我也尝试过这种方式:

<context:property-placeholder properties-ref="prop" />

<util:properties id="prop" location="reso"/>

<bean id="reso" class="org.springframework.core.io.FileSystemResource">
<constructor-arg index="0" value="${my.prop.file}" />
</bean>

我尝试过的一切都失败了。无论我将 my.prop.file 设置为什么。 HitTest 门的歌曲包括:

<context:property-placeholder location="/path/to/myConfigFile.properties" />
(ClassNotFoundException: .path.to.myConfigFile.properties)

<context:property-placeholder location="file:/path/to/myConfigFile.properties" />
(ClassNotFoundException: file:.path.to.myConfigFile.properties)

<context:property-placeholder location="file:///path/to/myConfigFile.properties" />
(ClassNotFoundException: file:...path.to.myConfigFile.properties)

如何在文件系统上而不是类路径上的位置使用属性占位符?我们使用的是 Spring 3.0.5。

事实证明,运行加载 spring 文件的 Java 程序的脚本存在问题。感谢您的帮助。我将要求删除这个问题,因为原始代码毕竟有效。感谢您的帮助。

最佳答案

这确实对我有用:

<context:property-placeholder location="file:/path/to/myConfigFile.properties" />

但这(有趣的是)没有:

<context:property-placeholder location="#{ systemProperties['foo'] }" />

我明白了

java.io.FileNotFoundException: Could not open ServletContext resource [/#{ systemProperties['foo'] }]

您将无法使用属性占位符 ${..}使用 PropertyPlaceholderConfigurer 的定义。先有鸡后有蛋。

您始终可以继承 PropertyPlaceholderConfigurer 并让它做任何您想做的事情(例如,在 setLocations() 方法中调用 @PostConstruct。然后,不要使用 <context:property-placeholder>,而是使用:

<bean class="com.foo.MyPropertyPlaceholderConfigurer"/>

关于java - 如何为文件系统上的文件使用属性占位符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10540986/

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