what is dependency injection in java

dependency injection

Your Answer

The Dependency Injection is a design pattern that solves the following problems like: 

1.How can an application be independent of how its objects are created?
2.How can a class be independent of how the objects it requires are created?
3.How can the way objects are created be specified in separate configuration files?
4.How can an application support different configurations?

Dependency Injection makes our programming code loosely coupled.

When writing a complex Java application, application classes should be as independent as possible of other Java classes to increase the possibility to reuse these classes and to test them independently of other classes while unit testing. 

Dependency Injection helps in gluing these classes together and at the same time keeping them independent.
What is dependency injection exactly? 
 Here the dependency part translates into an association between two classes. For example, class A is dependent of class B.
 Now, let's look at the second part, injection. All this means is, class B will get injected into class A by the IoC(Inversion of Control).
In Spring framework, IOC container is responsible to inject the dependency. We provide metadata to the IOC container either by XML file or annotation.

Dependency injection can happen in the way of passing parameters to the constructor or by post-construction using setter methods.



With Regards
Kiran Tiwari
www.sevenmentor.com

Core Java

Didn't get the answer.
Contact people of Talent-Core Java directly by clicking here