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

前端 未结 5 932
暗喜
暗喜 2020-12-23 02:40

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

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

This puts an empty Str

5条回答
  •  抹茶落季
    2020-12-23 03:18

    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.

    
        
        
    
    

    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

    
    

    Tested with Spring 3.1.1

提交回复
热议问题