2013-08-05 108 views
3

我有一個application.xml文件(目錄= WEB-INF/application.xml中)讀取參數.XML文件

和我有一個jasperserver.properties文件(目錄= WEB-INF /內部/ jasperserver.properties)

這是jasperserver.properties文件

我想從application.xml文件讀取 「SERVICE_URL」 屬性

我該如何做?

回答

4

在application.xml中使用PropertyPlaceholderConfigurer

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
    <property name="location"> 
    <value>classpath:path/to/jasperserver.properties</value> 
    </property> 
</bean> 

加載屬性文件。 然後在您的application.xml使用$ {} SERVICE_URL替代值:

<bean class="your class"> 
    <property name="serviceURL"><value>${SERVICE_URL}</value></property> 
</bean> 
+0

我需要輸入完整的路徑名嗎?我不能使用服務器名稱或端口等,否則代碼將不會通用,因爲這些值會不斷變化(文件位於Servername:Port \ webapps \ jasperserver-pro \ WEB-INF \ internal \ jasperserver-pro.properties) – Andre

+0

不,我認爲'classpath:internal/jasperserver.properties'或'classpath:WEB-INF/internal/jasperserver.properties'會很好 –

+0

我得到java.io.FileNotFoundException錯誤。我嘗試了很多其他的路徑和雙重檢查一切,沒有運氣:( – Andre

1

我覺得你jasperserver.properties是不是在你的類路徑中。在bean定義的值標籤給出

刪除類路徑,下面是修改後的

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
    <property name="location"> 
    <value>/WEB-INF/internal/jasperserver.properties</value> 
    </property> 
</bean> 

那就試試吧

其他明智副本jasperserver.properties src的文件夾,並添加如下修改豆提到

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
    <property name="location"> 
    <value>classpath:jasperserver.properties</value> 
    </property> 
</bean> 

希望它會有所幫助。