2012-06-11 105 views
1

當我創建一個Java Spring應用程序時,spring.xml配置文件通常位於src文件夾中。當我將此應用程序導出爲可運行JAR時,spring.xml文件將進入JAR中。但我不希望提取jar,只想在必要時更改spring.xml的內容。配置文件位於JAR中。如何將配置文件放在JAR之外?

但是由於XML位於JAR內部,這意味着我在更改spring.xml的內容時正在更改JAR文件。如何解決這個問題? JAR外部是否有spring.xml?

+0

http://stackoverflow.com/questions/775389/accessing-properties-files-outside-the-jar – daker

回答

0

是的,你可以把spring.xml出JAR的,然後你就可以在幾個方面從你的應用程序中引用它,無論是作爲使用file:資源或與classpath:類路徑。

例如,在web.xml你現在可以這樣做:

<!-- if spring.xml is on the classpath --> 
<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value>classpath*:spring.xml</param-value> 
</context-param> 

...或:

<!-- if spring.xml is at a known location --> 
<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value>file:/tmp/spring.xml</param-value> 
</context-param> 

UPDATE

請注意,我在回答你的問題,但會推薦使用PropertyPlaceholderConfigurer來代替(假設您只想更改屬性)。否則,如果您的上下文將從根本上改變,那麼您最好將其保存在JAR中,其中版本化更改表示新的二進制文件。

+0

但我必須事先知道文件:/tmp/spring.xml,對吧? – CodeBlue

+0

我相信你也可以做一個相對的位置,但我不記得我的頭頂相對於什麼。 – opyate

相關問題