Tuesday, April 22, 2014

Dependency Injection in Spring

What is Dependency Injection?

In Layman's Language: In any Object oriented programming language, objects depends on each other and they collaborating on each others by calling their methods. Traditionally we create objects using new operator as shown below.

Lets say i have Employee class and this employee class has a reference of Department object, we create the department object inside Employee class like shown below

public class Employee{  
 private Department dept=new Department();  
 public void displayMessage(){  
 dept.displayDeptDetails();  
 }  
 }   
Did you notice any problem in the above code?

Yes.You guessed it correct. 

The code is tightly coupled.For example if i need to create the department object in another way or simply put if i need to change any changes to department object i need to change the Employee class and if the reference in say in 100 classes in my project i need to change in 100 classes and need to rebuild and redeploy it and possibly insert some bugs if you forget to modify some class(no one is perfect and as developers we keep making mistakes otherwise why the hell the testing teams required :P)

Spring provides solution for this problem in the form of dependency injection.Spring let us configure how the dependencies needs to be created in an xml file/annotation and it will inject the dependencies at run time.


A sample bean declarations in xml file


<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

   <bean id="employee" class="com.ramesh.Employee">
       <constructor-arg ref="department"/>
   </bean>                                                                         <bean id="department" class="com.ramesh.Department">                         </bean>                                                                   </beans>

We have different bean container implementations for BeanFactory or ApplicationContext.

E.g.:
package com.ramesh;  
 import org.springframework.context.ApplicationContext;  
 import org.springframework.context.support.ClassPathXmlApplicationContext;  
 public class EmployeeMain{  
 public staticvoidmain(String[]args){  
 ApplicationContextcontext=  
 new ClassPathXmlApplicationContext("EmployeeContext.xml");  
 Employee emp=(Employee)context.getBean("employee");  
 dept.displayDeptDetails();  
 }   
 }  When the Spring application reads this xml file it will instantiates the beans and assign a unique id as described in the bean configuration.Now the beans along with their dependencies are ready for use.Since the dependencies injected at run time this will be called Dependency Injection. Most people use Inversion of Control and Dependency Injection interchangeably.

 But IoC is a generic term meaning rather than having the application call the methods in a framework, the framework calls implementations provided by the application.

DI is a form of IoC, where implementations are passed into an object through constructors/setters/service look-ups, which the object will 'depend' on in order to behave correctly.


Thanks for reading!!!!!!!

No comments:

Post a Comment