Dependency Injection In Spring boot

Reading Time: 3 minutes

It is a design pattern that helps in executing the Inversion of Control. The depth of dependency injection is for compelling the dependencies.

Dependency Injection is the main objective provided by Spring IOC. Here IOC stands for Inversion of Control.

The Spring module is responsible for injecting dependencies. The design principle of Inversion of Control focuses on keeping the Java classes independent of each other.

The container frees them from object creation and maintenance. These classes are managed by Spring so they must follow the standard definition of Java-Bean.

In the Spring framework, there are mainly two modules that are Core container and dependency injection. The core container is the main part of Spring’S core container.

In a conventional way, Developers will have control over the code in creating the objects & injecting them at run time.

Now though it might appear okay to depend on one module on the other, in the real world, this could lead to a lot of problems, including system failure.

Hence, Dependency Injection makes the code easy and reusable.

Ways to use Dependency Injection:

Achieved in two different ways:

  • Constructor based Dependency Injection
  • Setter based Dependency Injection

Spring Framework benefits us in providing a way to detect the relationship between the beans by reading the XML Configuration file.

By scanning the Java annotations at the time of booting up the application.

Therefore, The task is taken up through Container – Bean Factory which will create the objects and wire the dependencies.

That’s why Spring Framework does this process automatically, it is referred to as Auto wiring.

1.) Constructor Based Dependency Injection:

When @Autowired is utilized on upmost of the class constructor is known as Constructor-based dependency injection.

POJO Class: EmployeeMasterDetails class

Controller: CompanyMasterDetails class

2) Setter based dependency injection

When @autowired is needed at the top of the setter method is called Setter based Dependency injection.

POJO Class: EmployeeMasterDetails class

Controller: CompanyMasterDetails class

Advantages of Dependency Injection

  • It benefits in accomplishing loosely coupled architecture by deleting the tight coupling.
  • It benefits the coders to test the module by interjecting the dependent Mock Objects.
  • Hence, It deletes unwanted dependencies between the classes.
  • The opportunity of making the factor reusable is very huge.
  • Maintenance of the System becomes easy.

Disadvantages of Dependency Injection

  • It makes it difficult to get track of the code because we need to refer to more files to understand how the system behaves.
  • Therefore, the Spring Framework takes care of the control.
  • Since it will be difficult for the developers to understand how things work in the background
  • So, It increases the number of Interfaces & classes.

Conclusion

In conclusion, We have covered the depth of Dependency Injection its advantages & disadvantages, and ways of performing dependency injection.

knoldus

Reference

https://www.educba.com/dependency-injection-in-spring/

https://www.geeksforgeeks.org/spring-dependency-injection-with-example

https://docs.spring.io/

Leave a Reply