Monthly Archives: August 2014

Using system properties in with Spring

This is actually pretty easy to pull off. First you must use a property-placeholder like this:

[xml]
<context:property-placeholder location="classpath:myapp.properties"
system-properties-mode="ENVIRONMENT"/>
[/xml]

The system-properties-mode supports several options including:

  • FALLBACK – Indicates placeholders should be resolved against any local properties and then against system properties
  • NEVER – Indicates placeholders should be resolved only against local properties and never against system properties
  • OVERRIDE – Indicates placeholders should be resolved first against system properties and then against any local properties
  • ENVIRONMENT – Indicates placeholders should be resolved against the current Environment and against any local properties. This is the default.

The location is an optional parameter so depending on your needs you can skip configuring it.

There are some other optional parameters available including:

  • ignore-resource-not-found – Specifies if failure to find the property resource location should be ignored. Default is “false”, meaning that if there is no file in the location specified an exception will be raised at runtime.
  • ignore-unresolvable – Specifies if failure to find the property value to replace a key should be ignored. Default is “false”, meaning that this placeholder configurer will raise an exception if it cannot resolve a key. Set to “true” to allow the configurer to pass on the key to any others in the context that have not yet visited the key in question.

A system property can then be utilized like this:

[xml]
<property name="username" value="#{systemProperties.username}"/>
[/xml]

The # is not a mistake; this indicates that the bean systemProperties should be used.