What is Dependency Injection in Spring?

What is Dependency Injection in Spring?

Ninja Asked on 17th September 2018 in Spring.
Add Comment
1 Answer(s)
Best answer

Software components (Client), are often a part of a set of collaborating components which depend upon other components (Services) to successfully complete a business function. The client object need to know which service objects to communicate with, where to locate them and how to communicate with them.

Dependency Injection is based on the principle of ‘Inversion of Control’. The idea is to avoid direct dependency between collaborating objects by not creating objects using ‘new’ operator. Dependency injection is a way of structuring code such that clients do not locate or instantiate services as part of usual logic. Rather the dependencies are provided in a configuration file / through annotations and an external component (in case of spring framework the IoC container) is responsible for hooking up the objects.

One of the biggest advantages of dependency injection is that it reduces coupling between objects thereby making testing a lot easier. Independent clients are easier to unit test in isolation using stub or mock objects.

Ninja Answered on 17th September 2018.
Add Comment