June 2018
Intermediate to advanced
408 pages
11h 23m
English
As we can see, Spring supports three types of DI methods; however, field-based dependency is not recommended by Spring. So, constructor-based and setter-based DI are standard ways to injecting beans in your application. The selection of constructor or setter methods depends on your application requirements. In this table, we will see the different use cases of constructor and setter injection, and some best practices that will help us decide when to use setter injection over constructor injection, and vice versa:
|
Constructor injection |
Setter injection |
|
Best choice when the dependency is mandatory. |
The suitable choice when the dependency is not mandatory. |
|
Constructor injection makes the bean ... |
Read now
Unlock full access