• Breaking News

    Wednesday, 24 September 2014

    What is Dependency Injection and How spring related to it

    Dependency injection means an object  should only have dependency which require by object to do their job. It means how much one object related to another job.





    Let , Take an Example Suppose

    I have a class Car

    1
    2
    3
    4
    5
    6
    public class Car{
     
     private void drive(){
      
     }
    }
    

    I have another class Bike

    1
    2
    3
    4
    5
    6
    public class Bike{
     
     private void drive(){
      
     }
    }
    

    I have main class

    1
    2
    3
    4
    5
    6
    7
    8
    public class MainClass{
     
     Car car=new Car();
     car.drive();
     
     Bike bike=new Bike();
     bike.drive();
    }

    so here my main class dependent upon car,and bike object.so what is another way of to solve this problem is polymorphism.

    I create an interface Drive

    1
    2
    3
    4
    public interface Drive{
     
     void drive();
    }
    

    and Car and Bike class implement Drive interface

    1
    2
    3
    4
    5
    class Car implements Drive{
     public void drive(){
      
     }
    }
    


    1
    2
    3
    4
    5
    class Bike implements Drive{
     public void drive(){
      
     }
    }
    


    1
    2
    3
    4
    5
    6
    7
    8
    9
    public class MainClass{
     
     Drive drive=new Car();
     drive.drive();
     
     Drive drive=new Bike();
     drive.drive();
     
    }
    

    In this when call drive method i don't now whether drive contain car or bike object but still have to write new Car() and new Bike() 

    Now we try another method below in this case driveVechile method completely independent of argument drive it may be car or bike. it will call drive method of car and bike dependent upon argument.

    1
    2
    3
    4
    5
    6
    7
    public class MainClass{
    
     public void driveVehicle(Drive drive){
      drive.drive();
     }
     
    }
    

    But still in this case has problem somewhere in this class we have to write code i.e


    1
    2
    3
    Drive drive=new Car();
    drive.drive();
     
    

    Now if i want to call drive method of bike class then i have to change code something like this


    1
    2
    3
    Drive drive=new Bike();
    drive.drive();
     
    

    So still this class code is hard coded which again create problem still our mainclass dependent upon what vechile it drive.so what we do we use concept of dependency injection in which spring framwork work. Now suppose we have Driving class.


     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    public class Driving{
     
     Drive drive;
    
     public void setDrive(Drive drive) {
      this.drive = drive;
     }
     
     public void drive(){
      this.drive.drive();
     }
     
    }
    

    This code should be written in another class


    1
    2
    3
    Car car=new Car();
    driving.setDrive(car);
    driving.drive();
    

    so in this case we see that driving class completly independent of what vehicle it drive .we inject the vehicle by using setter method of Driving class. it setter mehod take any vechile and drive that.

    so if we want to add another vechile then we don,t have to change driving class.this setter injection.

    Another type of dependency injection is Constructor injection


     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    public class Driving{
     
         Drive drive;
    
         public Driving(Drive drive){
          this.drive=drive;
         }
      
         public void drive(){
      this.drive.drive();
         }
     
    }
    

    So this is basic principal of spring framework.

    No comments:

    Post a Comment