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.
If no default value is declared explicitly, the default value is the null value. This usually makes sense because a null value can be considered to represent unknown data. In a table definition, default values are listed after the column data type.
To set a default value for primitive types such as boolean and int, we use the literal value: @Value("${some. key:true}") private boolean booleanWithDefaultValue; @Value("${some.
Method parameters annotated with @RequestParam are required by default. will correctly invoke the method. When the parameter isn't specified, the method parameter is bound to null.
In Spring dependency injection, we can inject null and empty values. In XML configuration, null value is injected using <null> element.
This is really old, but you can use Spring EL now, e.g.
@Value("${stuff.value:#{null}}")
See this question.
Thanks to @vorburger:
@Value("${email.protocol:#{null}}")
String protocol;
will set the string value at null without any other configurations.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With