我们曾经有一种方法可以从类路径上的文件中加载属性:
<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设置为什么。最大的成功包括:
<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程序的脚本出现了问题。谢谢你的帮助。我将要求删除这个问题,因为原来的代码毕竟是有效的。谢谢你的帮助。
发布于 2012-05-11 03:55:14
这条路怎么样?
<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>发布于 2012-05-11 11:49:27
这对我来说很管用:
<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并让它做任何您想做的事情(例如,在@PostConstruct方法中调用setLocations() )。然后,不使用<context:property-placeholder>,而使用:
<bean class="com.foo.MyPropertyPlaceholderConfigurer"/>https://stackoverflow.com/questions/10540986
复制相似问题