【问题标题】:How can I override a property from the command line in my spring webapp如何在我的 spring webapp 中从命令行覆盖一个属性
【发布时间】:2013-01-09 16:29:36
【问题描述】:

我有这个属性设置

<bean id="preferencePlaceHolder"
      class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
    <property name="locations" ref="propertiesLocations" />
</bean>
<beans profile="dev, testing">
    <util:list id="propertiesLocations">
        <value>classpath:runtime.properties</value>
        <value>classpath:dev.properties</value>
    </util:list>
</beans>
...

runtime.properties 里面有个属性是这样的

doImportOnStartup=false

我想偶尔这样做

mvn jetty:run -DdoImportOnStartup=true

并让系统属性优先。我怎样才能做到这一点?谢谢。

【问题讨论】:

    标签: spring properties


    【解决方案1】:

    这可能不是你想要的,但无论如何这是我的属性加载 xml。这些位置按顺序加载,因此最后找到的位置将覆盖较早的位置,因此首先是类路径(即战争),然后是文件系统上的 env 特定文件。我更喜欢这种方法,因为它是一个指向外部文件的 1 次配置,但您只需在需要时更改该外部文件,无需再配置 Spring 或 JVM 参数。最终位置是寻找一个 -Dconfig JVM 参数,您可以在其中提供覆盖道具文件的完整路径。

    希望这会有所帮助。

    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="ignoreUnresolvablePlaceholders" value="true" />
    </bean>
    <bean class="org.springframework.web.context.support.ServletContextPropertyPlaceholderConfigurer">
        <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
        <property name="searchContextAttributes" value="true" />
        <property name="contextOverride" value="true" />
        <property name="ignoreResourceNotFound" value="true" />
        <property name="locations">
            <list>
                <value>classpath:*.properties</value>
                <value>file:${HOME}/some-env-specific-override.properties</value>
                <value>${config}</value>
            </list>
        </property>
    </bean>
    

    【讨论】:

    • 这对我有用,除了我真正需要的唯一一点是:&lt;property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" /&gt; 并且可以在 PropertyPlaceholderConfigurer 而不是 ServletContextPropertyPlaceholderConfigurer 上设置。我也不清楚为什么你需要两者,因为 ServletContextPropertyPlaceholderConfigurer 无论如何都继承自 PropertyPlaceholderConfigurer?
    猜你喜欢
    • 1970-01-01
    • 2016-08-31
    • 2012-12-02
    • 2016-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多