Spring is most known with is the Dependency Injection (DI) flavor of Inversion of Control(IOC).
Spring, Google Guice and Java EE CDI frameworks facilitate the process of dependency injection through use of Java Reflection API and java annotations. All we need is to annotate the field, constructor or setter method and configure them in configuration xml files or classes.
Differentiating with dependency injection
Inversion of control is a design paradigm with the goal of giving more control to the targeted components of your application, the ones getting the work done.
Dependency injection is a pattern used to create instances of objects that other objects rely on without knowing at compile time which class will be used to provide that functionality. Inversion of control relies on dependency injection because a mechanism is needed in order to activate the components providing the specific functionality.
The two concepts work together in this way to allow for much more flexible, reusable, and encapsulated code to be written. As such, they are important concepts in designing object-oriented solutions.
Implementing inversion of control design pattern
In object-oriented programming, there are several basic techniques to implement inversion of control. These are:
- using a factory pattern
- using a service locator pattern
- using a dependency injection
There are 3 types of dependency Injection.
- Constructor Injection : Dependencies are provided as constructor parameters.
- Setter Injection : Dependencies are assigned through JavaBeans properties (ex: setter methods).
- Interface Injection: Injection is done through an interface.