"program to interface not implementation"
IoC and DIBoth words are used interchangeably in Java world but there is a difference.
Inversion of Control is a more general concept
Dependency Injection is a concrete pattern
IoC - Inversion of ControlInverts responsibility of managing object life cycle (create, initialize, destroy, etc). In simple term, let someone else to create objects for you instead of using "new" in code itself. Normally this someone else is Ioc Container.
IoC ContainerFramework to create dependencies and inject them automatically when required.
DI - Dependency InjectionObjects are depending on other Objects. In DI, Objects are given (injects) their dependencies (other objects) through their constructors, methods, fields.
IoC with DISo if DI means objects are injected who is doing that. Here is where IoC comes in to play. With IoC dependencies are managed by the container.
Spring FrameworkSpring Framework is the most famous Inversion of control container for Java applications.
The Spring IoC container is responsible for managing life cycles of specific objects. Creating these objects, calling their initialization methods and configuring these objects by wiring them together.
These objects are typically called as beans.
BeanFactory container - simplest container providing basic support for DI.
ApplicationContext container - adds more enterprise specific functionalities.
- Reduce coupling
- Improve testability