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

I have this property setting

<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

 doImportOnStartup=false 

I would like to do it sometimes

 mvn jetty:run -DdoImportOnStartup=true 

and have a system property. How can i achieve this? Thanks.

+6
source share
1 answer

It may not be exactly what you want, but here is my loading of the xml properties anyway. Places are loaded in order, so the last one will override the previous one, therefore, the class path (i.e. War) first, followed by env of specific files in the file system. I prefer this approach because its one-time configuration points to an external file, but you just modify this external file as necessary, no longer configure the Spring or JVM arguments. In the final place, search for -Dconfig JVM arg, which you give the full path for the prop override 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> 
+3
source

All Articles