【问题标题】:Configuration file is within the JAR. How to put the configuration file outside the JAR?配置文件在 JAR 中。如何将配置文件放在 JAR 之外?
【发布时间】:2012-06-11 21:36:31
【问题描述】:

当我创建 Java Spring 应用程序时,spring.xml 配置文件通常位于 src 文件夹中。当我将此应用程序导出为可运行的 JAR 时,spring.xml 文件会进入 JAR。但我不希望提取 jar,只希望在必要时更改 spring.xml 的内容。

但是由于 XML 在 JAR 中,这意味着我在更改 spring.xml 的内容时正在更改 JAR 文件。如何解决这个问题?是否可以将 spring.xml 放在 JAR 之外?

【问题讨论】:

标签: java spring


【解决方案1】:

是的,您可以将 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>

更新

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

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

【讨论】:

  • 但是我得提前知道 file:/tmp/spring.xml 对吧?
  • 我相信你也可以做一个相对位置,但我不记得我的头顶相对于什么。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-12-26
  • 2018-11-11
  • 2013-01-07
  • 2018-04-22
  • 2011-01-15
  • 2015-11-30
  • 2017-04-30
相关资源
最近更新 更多