如何在spring webapp中从命令行覆盖属性

Oll*_*rds 6 spring properties

我有这个属性设置

<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>
...
Run Code Online (Sandbox Code Playgroud)

runtime.properties中有一个属性,如此

doImportOnStartup=false
Run Code Online (Sandbox Code Playgroud)

我想偶尔这样做

mvn jetty:run -DdoImportOnStartup=true
Run Code Online (Sandbox Code Playgroud)

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

shu*_*tsy 3

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

希望这可以帮助。

<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>
Run Code Online (Sandbox Code Playgroud)