Take the 2-minute tour ×

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

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.

share|improve this question

2 Answers 2

up vote 17 down vote accepted

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.

    <!-- config the location(s) of the properties file(s) here -->
    <property name="nullValue" value="@null" />

Now you can use the string @null to represent the null value

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

share|improve this answer
works like a charm, thanks! –  Kevin Schmidt Aug 17 '12 at 11:21
What's Java configuration equivalent of this? –  Kumar Sambhav Aug 21 at 6:53
stackoverflow.com/questions/9347929/… mentions using ${some.param:#{null}}, and that worked for me without having to set nullValue, seems that this is the default? (In a Spring Boot application.) –  vorburger Oct 5 at 21:43

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;

   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.

public class MyObject {

    private Crap crap;

    public enum Crap {

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


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

share|improve this answer
The String value was an example, I really needed this for an Enum value, where an empty String just causes a conversion error. With the @null you can have a null as "no Enum set", which is what I need. –  Kevin Schmidt Aug 17 '12 at 11:23
All the more reason why I would not recommend null but instead some enum that represents not set. –  Adam Gent Aug 17 '12 at 12:07
That would require putting Enum.NOTSET into all properties files (>30), which seems a lot of work with almost no benefits... –  Kevin Schmidt Aug 20 '12 at 11:02
I haven't tried this but in theory wouldn't @Value("${stuff.value:NOTSET}") work? –  Adam Gent Aug 20 '12 at 13:47
@KevinSchmidt See my updated answer. –  Adam Gent Aug 20 '12 at 13:58

Your Answer


