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