如何在文件系统上使用property-placeholder文件 [英] How to use property-placeholder for file on filesystem

查看:130
本文介绍了如何在文件系统上使用property-placeholder文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们曾经有办法从类路径上的文件加载属性:

We used to have a way to load properties from a file on the classpath:

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

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

and it worked great. But now we want to load properties from a specific file on the system that is NOT in the classpath. We wanted to be able to dynamically load the file, so we are using a Java environment variable to populate it. I'll give a simple example below:

在Java中:

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

在Spring XML中:

In Spring XML:

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

我也是这样尝试过的,感谢Luciano的一个想法:

I've also tried it this way, thanks to an idea from 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设置为什么。最热门的点击包括:

Everything I've tried has failed. No matter what I set my.prop.file to. Greatest hits include:

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

How do you use property-placeholders with a location that is on the file system and NOT on the classpath? We are using Spring 3.0.5.

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

It turns out there was a problem with the script running the Java program that loads the spring file. Thank you for helping. I am going to request that this question be deleted, as the original code works after all. Thank you for your help.

推荐答案

这种方式怎么样?

<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="/yourpathandfile" />
</bean>

这篇关于如何在文件系统上使用property-placeholder文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆