Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I override a property from the command line in my spring webapp

I have this property setup

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

There is a property in runtime.properties like so

doImportOnStartup=false

I would like to occasionally do this

mvn jetty:run -DdoImportOnStartup=true

and have the system property take precedence. How can I achieve this? Thanks.

like image 208
Ollie Edwards Avatar asked Jan 25 '13 16:01

Ollie Edwards


1 Answers

This might not be exactly want you want, but here's my property loading xml anyway. The locations are loaded in order, so the last found will override earlier one, hence the classpath (i.e. war) one first followed by env specific files on the file system. I prefer this approach as its a 1 time config to point at an external file, but you just change that external file as and when required, no more configuring of Spring or JVM args. The final location is looking for a -Dconfig JVM arg which you give a full path for the override prop file.

Hope this helps.

<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>
like image 63
shuttsy Avatar answered Nov 15 '22 05:11

shuttsy