161

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.

Kevin Schmidt
  • 2,111
  • 2
  • 14
  • 12

5 Answers5

335

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

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

See this question.

Stefan Falk
  • 23,898
  • 50
  • 191
  • 378
ben3000
  • 4,629
  • 6
  • 24
  • 43
89

Thanks to @vorburger:

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

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

Kirill Ch
  • 5,496
  • 4
  • 44
  • 65
80

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

nosebrain
  • 1,072
  • 1
  • 15
  • 18
  • 2
    What's Java configuration equivalent of this? – Kumar Sambhav Aug 21 '14 at 06:53
  • 37
    http://stackoverflow.com/questions/9347929/spring-set-a-property-only-if-the-value-is-not-null 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 05 '14 at 21:43
  • 7
    Simple use power of SPEL ${stuff.value:#{null}} as suggested by vorburger and in http://stackoverflow.com/questions/16735141/specify-default-property-value-as-null-in-spring – csf Sep 30 '15 at 16:41
  • seems much less configuration is required for ben3000 and JRichardsz answers. – Jeff Apr 29 '20 at 11:24
  • there is a newer way to set `null` as default, see the most up'ed answer here – Dáve Feb 25 '21 at 13:54
2

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

Adam Gent
  • 47,843
  • 23
  • 153
  • 203
  • 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
  • 3
    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
  • Yes, this is a good solution. Unfortunately I can't change the enum (dep), so I have to go with null. Otherwise you are right, the NOTSET would work. – Kevin Schmidt Aug 22 '12 at 13:17
  • How about assigning an integer value? @Value("${datasource.connections:5}") private Integer connections; would this work? – yousafsajjad Jul 10 '14 at 15:38
0

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

ylev
  • 2,313
  • 1
  • 23
  • 16
  • 1
    the initial `@Value("${stuff.value:}") private String value;` does exactly the same and is preferred way of doing it. Anyway your reply does not answer the question. – Andrew T Nov 22 '21 at 22:48