簡體   English   中英

如何在文件系統上使用property-placeholder文件

[英]How to use property-placeholder for file on filesystem

我們曾經有辦法從類路徑上的文件加載屬性:

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

事實證明,運行Java程序的腳本存在加載spring文件的問題。 謝謝你的幫忙。 我將要求刪除此問題,因為原始代碼畢竟有效。 謝謝您的幫助。

這對我有用:

<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>而不是使用<context:property-placeholder>

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

這個怎么樣?

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM