如何从SpringWebApp中的命令行重写属性

如何从SpringWebApp中的命令行重写属性,spring,properties,Spring,Properties,我有这个属性设置 <bean id="preferencePlaceHolder" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer"> <property name="locations" ref="propertiesLocations" /> </bean> <beans profile="dev, testing"&

我有这个属性设置

<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>
...
我想偶尔这样做

mvn jetty:run -DdoImportOnStartup=true

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

这可能不是您想要的,但这是我加载xml的属性。这些位置是按顺序加载的,因此最后一个找到的位置将覆盖前面的位置,因此类路径(即war)首先是文件系统上的特定于环境的文件。我更喜欢这种方法,因为它是指向外部文件的一次配置,但您只需在需要时更改该外部文件,不再配置Spring或JVM参数。最后一个位置是寻找一个-dconfigJVM参数,您可以为覆盖属性文件提供完整的路径

希望这有帮助

<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>

类路径:*.properties
文件:${HOME}/some-env-specific-override.properties
${config}

这对我来说很有效,除了我真正需要的是:
而且可以在PropertyPlaceHolderConfigure而不是ServletContextPropertyPlaceHolderConfigure上设置。因为ServletContextPropertyPlaceholderConfigurer继承了PropertyPlaceholderConfigurer,所以我也不清楚为什么需要这两种配置?
<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>