The basic concept of the Inversion of Control pattern (also known as dependency injection) is that you do not create your objects but describe how they should be created. You don't directly connect your components and services together in code but describe which services are needed by which components in a configuration file. A container (in the case of the Spring framework, the IOC container) is then responsible for hooking it all up.
Dependency Injection is a way to let the framework or the container works out the complexities of bean instantiation, initialization and sequencing and supplies the bean references to the clients as required. In spring, IOC has a responsibility of doing DI.
Benefit of IOC(Dependency Injection)
Benefits of IOC (Dependency Injection) are as follows:
Loose coupling: We do not need to write codes to use the factory method pattern to manage the beans,
instead the container manages the bean life cycle. So the application code is free from managing
the beans. As a result of this the amount of code is reduced and we can concentrate on the business logic.
Better Design: It promotes to "program to an interface not an implementation".
Testability: Application code becomes easier to test using mock objects instead of actual objects.
Ease of development: It becomes easier to add, remove or alter the services just changing the configuration.
Types of IOC (Dependency Injection)
There are two types of Dependency Injection
Constructor-based DI is accomplished when the container invokes a class constructor with a number of arguments,
each representing a dependency on other class.
Setter-based DI is accomplished by the container calling setter methods on your beans after invoking a no-argument
constructor or no-argument static factory method to instantiate your bean.
Example of Setter Injection