Reputation: 1042
I have an Integer autowired in constructor like that:
@Value("${application.someNumber:0}") Integer number)
And there is NullPointerException if nmber is null in configuration. How to write this expression so that if it is null then it would be set to 0 ? Then I would throw my own validation expetion somewhere else.. ?
Upvotes: 0
Views: 713
Reputation: 2027
If you want to use @Value
annotation in the constructor, you must annotate the constructor itself with @Autowired
. Also the class must be a spring managed bean (component, service...)
Upvotes: 1
Reputation: 7371
First you should remove the )
at the end. As for your question. You can simply assign 0 to number
like so:
Integer number = 0;
When the injected value is null number
will keep the original value of 0
, if it is not null it will get the injected value
Upvotes: 0