Can I set null as the default value for a @Value in Spring?

JavaSpringSpring Annotations

Java Problem Overview


I'm currently using the @Value Spring 3.1.x annotation like this:

@Value("${stuff.value:}")
private String value;

This puts an empty String into the variable if the attribute is not present. I would like to have null as the default instead of an empty String. Of course I also want to avoid an error when the property stuff.value is not set.

Java Solutions


Solution 1 - Java

This is really old, but you can use Spring EL now, e.g.

@Value("${stuff.value:#{null}}")

See this question.

Solution 2 - Java

Thanks to @vorburger:

@Value("${email.protocol:#{null}}")
String protocol;

will set the string value at null without any other configurations.

Solution 3 - Java

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

Solution 4 - Java

I give @nosebrain credit as I did not know about "null-value" but I prefer to avoid using null values altogether particularly since its difficult to represent null in a properties file.

But here is an alternative using null with out null-value so it will work with what ever property placeholder.

public class MyObject {

   private String value;

   @Value("${stuff.value:@null}")
   public void setValue(String value) {
      if ("@null".equals(value)) this.value = null;
      else this.value = value;
   }
}

Personally I prefer my way because maybe later on you want stuff.value to be a Comma-separated-value or perhaps to Enum the switch is easier. Its also easier to unit test :)

EDIT: based on your comments on using enums and my opinion of not using null.

@Component
public class MyObject {

	@Value("${crap:NOTSET}")
	private Crap crap;
	
	public enum Crap {
		NOTSET,
		BLAH;
	}
}

The above works fine for me. You avoid null. If your property files want to explicit set that they don't want to handle it then you do (but you don't even have to specify this as it will default to NOTSET).

crap=NOTSET

null is very bad and is different than NOTSET. It means spring or unit test did not set it which is why there is IMHO a difference. I still would probably use the setter notation (previous example) as its easier to unit test (private variables are hard to set in a unit test).

Solution 5 - Java

In case you need to inject an empty (0 length) "" string as @Value default - use SPEL (spring expression language) as follows:

@Value("${index.suffix:#{''}}") 
private String indexSuffix;

#{''} just gets you an empty string as injected @Value's default.

by yl

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKevin SchmidtView Question on Stackoverflow
Solution 1 - Javaben3000View Answer on Stackoverflow
Solution 2 - JavaKirill ChView Answer on Stackoverflow
Solution 3 - JavanosebrainView Answer on Stackoverflow
Solution 4 - JavaAdam GentView Answer on Stackoverflow
Solution 5 - JavaylevView Answer on Stackoverflow