Which one is better, setter injection or constructor injection?
Constructor injection is better.
If you want to prompt the class, you always do it with the constructor. When you are using the constructor-based injection, the only way to prompt or instantiate the class is by using the constructor. If you pass the dependency via a constructor, it becomes obvious that it is an essential dependency.
However, if you use the setter injection in the POJO class, you might face challenges in setting the value of your class variable by using the setter method. This is entirely based on the customer's needs which is optional. If you pass the dependency via setter, it means that it is an optional dependency.
BY Best Interview Question ON 03 May 2022