2011-11-25 58 views
23

我有一個Spring應用程序,它的運行良好。現在我想要外部配置文件夾中的屬性文件,而不是在打包的jar文件中進行更改,而不需要重新打包。這是我得到了什麼:Spring應用程序上下文外部屬性?

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
<!-- <property name="locations" value="classpath:/springcontext.properties"/> --> 
<property name="locations" value ="config/springcontext.properties" /> 

的outcommented一個正在工作,另一個我不上班:/有人能幫忙嗎?

編輯: Thx目前爲止4評論。

也許我的問題不夠明確:)。我執行一個Maven構建,一切都將被打包,並且我希望此文件夾不在outcomming jar旁邊的包裝螺母中,並且在此文件夾中我想要屬性文件。可能?

+0

嘗試用奇異'location',雖然'locations'應該管用。也可以嘗試從類路徑引用中刪除前導'/'。 –

+0

任何人都可以幫助解決新的問題? –

+0

@DennisIch你是如何解決這個問題的?我正面臨類似的問題 – happybuddha

回答

11
<context:property-placeholder location="classpath*:spring/*.properties" /> 

如果你的地方放置在一個名爲彈簧類路徑(更改名稱/相應迪爾斯),你可以用上面

<property name="locations" value ="config/springcontext.properties" /> 

這將指向WEB-INF/classes下訪問/config/springcontext.properties

+0

它不是一個Web應用程序,它與一些命令行功能是獨立的,爲此我需要它在jar外。 –

+0

那麼它就在你的jar文件的旁邊,如果它不是一個web項目 – fmucar

2

一種方法是將你的外部配置文件夾添加到java進程的類路徑中。這就是我過去經常這樣做的方式。

+0

看來Spring的'財產placeholder'不能夠在那裏裝載應用程序上下文定義包外財產的文件(見http://forum.springsource.org/showthread.php?87994-Classpath*-in-上下文屬性佔位符-犯規-拾取系統類路徑&p = 295698#post295698)。你是如何實現加載外部屬性文件的? –

+0

您是否嘗試將其添加到清單文件? – pap

+1

嗯...將什麼添加到清單文件? MANIFEST.MF文件駐留在一個包中,它如何幫助? –

11

您可以使用文件前綴來加載外部應用程序上下文文件中的一些這樣的事

<context:property-placeholder location="file:///C:/Applications/external/external.properties"/> 
+0

也可以作爲'PropertyPlaceholderConfigurer'的'location'屬性 –

43

你可以嘗試這樣的事情:

<context:property-placeholder 
     location="${ext.properties.dir:classpath:}/servlet.properties" /> 

而在你的應用服務器/ JVM定義ext.properties.dir財產,否則默認屬性位置「類路徑:/」(名.jar即類目錄或的.war)將被使用:

-Dext.properties.dir=file:/usr/local/etc/ 

順便說一句,非常有用blog post

9

This博客可以幫助你。訣竅是使用SpEL(Spring表達式語言)來讀取像user.home這樣的系統屬性,使用SpEL讀取用戶主目錄,您可以在您的bean元素內部使用
#{ systemProperties['user.home']}表達式。例如,要訪問存儲在主目錄中的屬性文件,您可以在PropertyPlaceholderConfigurer中使用以下內容,它對我很有用。

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
    <property name="locations"> 
     <value>file:#{ systemProperties['user.home']}/ur_folder/settings.properties</value> 
    </property> 
</bean> 
5

這個問題有點古怪,但想分享一些對我有用的東西。希望對於在外部位置搜索某些信息訪問屬性的用戶有用。

這是爲我工作。

  1. 屬性文件內容:

    PROVIDER_URL=t3://localhost:8003,localhost:8004 
    
  2. applicationContext.xml文件的內容:(春季3.2。3)

    注:${user.home}是從OS的系統性能。

    <context:property-placeholder system-properties-mode="OVERRIDE" location="file:${user.home}/myapp/latest/bin/my-env.properties"/> 
    
    <bean id="appsclusterJndiTemplate" class="org.springframework.jndi.JndiTemplate"> 
        <property name="environment"> 
         <props> 
          <prop key="java.naming.factory.initial">weblogic.jndi.WLInitialContextFactory</prop> 
          <prop key="java.naming.provider.url">${PROVIDER_URL}</prop> 
         </props> 
        </property> 
    </bean> 
    

${PROVIDER_URL}得到的屬性與價值替換的文件

1
<context:property-placeholder location="file:/apps/tomcat/ath/ath_conf/pcr.application.properties" /> 

這對我的作品。 本地開發機器路徑是C:\ APPS \ tomcat的\ ATH \ ath_conf和服務器/應用/ tomcat的/ ATH/ath_conf

這兩部作品對我來說

相關問題