Constructor vs Setter dependency Injection in Spring
We need the assurance from the Inversion of control (IoC) container that, before using any bean, the injection of necessary
beans must be done.
In setter injection strategy, we trust the Inversion of control (IoC) container that it will first create the bean first
but will do the injection right before using the bean using the setter methods. And the injection is done according to your configuration.
If you somehow misses to specify any beans to inject in the configuration, the injection will not be done for those beans and your
dependent bean will not function accordingly when it will be in use!
But in constructor injection strategy, container imposes (or must impose) to provide the dependencies properly while constructing
the bean. This was addressed as "container-agnostic manner", as we are required to provide dependencies while creating the bean, thus making
the visibility of dependency, independent of any IoC container.
|