software engineering, dependency injection is a technique whereby one object (or static method) supplies the dependencies of another object. ... In Java, before we can use methods of other classes, we first need to create the object of that class (i.e. class A needs to create an instance of class B).
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.
its an concept of IOC where we can inject an object into dependent class either using setter method or constructor.this we can configure either through spring configuration file.
A framework class, usually called the dependencycontainer, could analyze the dependencies of this class. With this analysis it is able to create an instance of the class and inject the objects into the defineddependencies, via Java reflection.
The general concept behind dependency injection is called Inversion of Control. According to this concept a class should not configure its dependencies statically but should be configured from the outside