I develop application and found something I cant understand. I have created my custom validation annotation called @UsernameAvailable and implementation of ConstraintValidator.
I annotate some field of my entity with the annotation and I have added some System.out.println("something message"); inside ConstraintValidator isValid() method.
What I have noticed is that the validation method is called twice:
At first while the form on the page is submited and data is binded to entity. And then, second time when the entity is saved by JpaRepository (from Spring Data JPA)
But the difference is that, in my ConstraintValidator implementation I have some @Autowired dependencies and when validation takes place for the first time on "form submit validation" dependencies are injected properly.
And at the second time, while the isValid method is called on saving entity my @Autowired Service is null, why is that?
If you dont understand just read and watch that:
All of what I wrote is shown on this video: https://www.youtube.com/watch?v=rFf0CaxaHVc (best, start watching from 04:20 minute)
This is exact problem I have, why do I have to check nulls in isValid? Why is the service null?
Its quite imporant for me to understand that