<<< Prev

Next >>>

Dependency Injection(IOC) in Spring

Applying IoC, objects are given their dependencies at creation time by some external entity that coordinates each object in the system. That is, dependencies are injected into objects. So, IoC means an inversion of responsibility with regard to how an object obtains references to collaborating objects.


Benifits of Dependency Injection in Spring:

  • 1. Ensures configuration and uses of services are separate.
  • 2. Can switch implementations by just changing configuration.
  • 3. Enhances Testability as mock dependencies can be injected.
  • 4. Dependencies can be easily identified.
  • 5. No need to read code to see what dependencies your code talks to.

Types of Dependency Injection:

  • 1. Setter Injection: Setter-based DI is realized by calling setter methods on your beans after invoking a no-argument constructor or no-argument static factory method to instantiate your bean.
  • 2. Constructor Injection: Constructor-based DI is realized by invoking a constructor with a number of arguments, each representing a collaborator.
  • 3. Interface Injection: In interface-based dependency injection, we will have an interface and on implementing it we will get the instance injected.