1

When I create a Java Spring application, the spring.xml configuration file is usually in the src folder. When I export this application as a runnable JAR, the spring.xml file goes inside the JAR. But I don't want the jar to be extracted and only want the contents of spring.xml to be changed when necessary.

But since the XML is inside the JAR, it means I am changing the JAR file when I change the contents of spring.xml. How to get around this? Is it possible to have the spring.xml outside the JAR?

4

1 回答 1

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>

更新

请注意,我正在回答您的问题,但建议您使用 aPropertyPlaceholderConfigurer代替(假设您只想更改属性)。

否则,如果您的上下文将发生根本性的变化,您不妨将其保存在 JAR 中,其中版本化更改表示一个新的二进制文件。

于 2012-06-11T21:43:46.457 回答