0

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

michealAtmi
  • 1,012
  • 2
  • 14
  • 36

2 Answers2

1

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

Smile
  • 3,832
  • 3
  • 25
  • 39
zlaval
  • 1,941
  • 1
  • 10
  • 11
0

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

Maurice
  • 6,698
  • 9
  • 47
  • 104