What are dependency injection and its advantages
The concept of dependency injection is that it provides a way for objects to define their dependencies (with the other objects they work with) only through constructor arguments, arguments to a factory method, or properties which are set on the object instance when it is being constructed or returned from a factory method. So container actually injects those dependencies when it creates the bean.
Hence code gets much cleaner when the DI principle is applied, and a higher degree of decoupling is much easier when beans do not look up their dependencies.
There are two types of Dependency Injection:
- Setter Injection
- Constructor Injection