What is dependency Injection (DI) and Inversion of Control (IOC)

  • Dependency Injection (DI) is a software design pattern.
  • According to DI all the components should be as independent as possible with other Java classes
  • This increases the possibility of reusing the class and testing them independently
  • In the above example if class A needs Object of class B and class C, Class A has to create objects of class B and class C. But in IOC using DI, the object of B and C is created by the Spring container for class A
  • So in nutshell rather than A configuring itself by creating object for class B and C, class B and class C object are configured by the container

Advantages :

  • IOC makes it loosely coupled
  • We provided the Information to spring container by XML or by annotations
  • There are two ways of injecting dependency
    • Constructor Injection
    • Dependency Injection

  • Autowiring feature of the spring framework enables you to inject object dependency implicitly. It internally uses setter or constructor injection.

Leave a Comment