Skip to content

Instantly share code, notes, and snippets.

@cherniag
Created August 11, 2017 11:02
Show Gist options
  • Save cherniag/e913ca5c4754694333d067c802fcbed4 to your computer and use it in GitHub Desktop.
Save cherniag/e913ca5c4754694333d067c802fcbed4 to your computer and use it in GitHub Desktop.
default null value in Spring @value
You must set the nullValue of the PropertyPlaceholderConfigurer. For the example I'm using the string @null but you can also use the empty string as nullValue.
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<!-- config the location(s) of the properties file(s) here -->
<property name="nullValue" value="@null" />
</bean>
Now you can use the string @null to represent the null value
@Value("${stuff.value:@null}")
private String value;
Please note: The context name space doesn't support the null value at the moment. You can't use
<context:property-placeholder null-value="@null" ... />
Tested with Spring 3.1.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment