W3Schools Learner's Blog

W3Schools Programming knowledge summary website



Dependency Injection in Spring

Dependency Injection in Spring

Dependency Injection (DI) is a design pattern that removes the dependency from the programming code so that it can be easy to manage and test the application. Dependency Injection makes our programming code loosely coupled. To understand the DI better, Let's understand the Dependency Lookup (DL) first:

Dependency Lookup
The Dependency Lookup is an approach where we get the resource after demand. There can be various ways to get the resource for example:
  1. A obj = new AImpl();  
In such way, we get the resource(instance of A class) directly by new keyword. Another way is factory method:
  1. A obj = factory.getA();  
This way, we get the resource (instance of A class) by calling the static factory method getA().
Alternatively, we can get the resource by JNDI (Java Naming Directory Interface) as:
  1. Context ctx = new InitialContext();  
  2. Context environmentCtx = (Context) ctx.lookup("java:comp/jndi");  
  3. A obj = (A)environmentCtx.lookup("A");  
There can be various ways to get the resource to obtain the resource. Let's see the problem in this approach.

Problems of Dependency Lookup

There are mainly two problems of dependency lookup.

  • tight coupling The dependency lookup approach makes the code tightly coupled. If resource is changed, we need to perform a lot of modification in the code.
  • Not easy for testing This approach creates a lot of problems while testing the application especially in black box testing.

    Dependency Injection

    The Dependency Injection is a design pattern that removes the dependency of the programs. In such case we provide the information from the external source such as XML file. It makes our code loosely coupled and easier for testing. In such case we write the code as:
    1. class Employee{  
    2. Address address;  
    4. Employee(Address address){  
    5. this.address=address;  
    6. }  
    7. public void setAddress(Address address){  
    8. this.address=address;  
    9. }  
    11. }  
    In such case, instance of Address class is provided by external souce such as XML file either by constructor or setter method.

    Two ways to perform Dependency Injection in Spring framework

    Spring framework provides two ways to inject dependency
    • By Constructor
    • By Setter method

No comments:

Post a Comment

Note: only a member of this blog may post a comment.