0 votes
in Spring by
What do you understand by Dependency Injection?

2 Answers

0 votes
by
Dependency Injection design pattern allows us to remove the hard-coded dependencies and make our application loosely coupled, extendable and maintainable. We can implement dependency injection pattern to move the dependency resolution from compile-time to runtime.

Some of the benefits of using Dependency Injection are Separation of Concerns, Boilerplate Code reduction, Configurable components, and easy unit testing.
0 votes
by

The main idea in Dependency Injection is that you don't have to create your objects but you just have to describe how they should be created.

The components and services need not be connected by us in the code directly. We have to describe which services are needed by which components in the configuration file. The IoC container present in Spring will wire them up together.

In Java, the 2 major ways of achieving dependency injection are:

Constructor injection: Here, IoC container invokes the class constructor with a number of arguments where each argument represents a dependency on the other class.

Setter injection: Here, the spring container calls the setter methods on the beans after invoking a no-argument static factory method or default constructor to instantiate the bean.

Related questions

0 votes
asked Apr 4, 2021 in Spring by Robindeniel
0 votes
asked Dec 31, 2023 in Angular by DavidAnderson
...