Pages

Wednesday 3 April 2013

Spring - Dependency Injection

Dependency Injection : as the term itself indicates it is injecting dependencies.
In terms of java simple words - providing state full dependent java object to the object which has dependency.
ex:
Class A{
B b;
}
here, providing object b with full state to object of class A.
Lets see in detail
Observe the below example - computer object has dependency on computerDetails object
package com.kb.spring.two.five;
import java.util.Date;
public class Computer {
private String productNo;
private double cost;
private Date purchaseDate;
private ComputerDetails computerDetails;
public Computer() {
System.out.println("computer default constructor");
}
public String getProductNo() {
return productNo;
}
public void setProductNo(String productNo) {
this.productNo = productNo;
}
public double getCost() {
return cost;
}
public void setCost(double cost) {
this.cost = cost;
}
public Date getPurchaseDate() {
return purchaseDate;
}
public void setPurchaseDate(Date purchaseDate) {
this.purchaseDate = purchaseDate;
}
public ComputerDetails getComputerDetails() {
return computerDetails;
}
public void setComputerDetails(ComputerDetails computerDetails) {
this.computerDetails = computerDetails;
}
}
now look at computerDetails class
package com.kb.spring.two.five;
public class ComputerDetails {
private int ramSize;
private int hardDiskSize;
private double processorSpeed;
public ComputerDetails() {
System.out.println("computerDetails default constructor");
}
public int getRamSize() {
return ramSize;
}
public void setRamSize(int ramSize) {
this.ramSize = ramSize;
}
public int getHardDiskSize() {
return hardDiskSize;
}
public void setHardDiskSize(int hardDiskSize) {
this.hardDiskSize = hardDiskSize;
}
public double getProcessorSpeed() {
return processorSpeed;
}
public void setProcessorSpeed(double processorSpeed) {
this.processorSpeed = processorSpeed;
}
}
Now look at the client program without using Spring Dependency Injection
package com.kb.spring.two.five;
import java.util.Date;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
public class ClientWithoutSpring {
public static void main(String[] args) {
Computer computer = new Computer();
computer.setCost(37500.00);
computer.setProductNo("123");
computer.setPurchaseDate(new Date());
ComputerDetails computerDetails = new ComputerDetails();
computerDetails.setRamSize(2);
computerDetails.setHardDiskSize(500);
computerDetails.setProcessorSpeed(3.2);
computer.setComputerDetails(computerDetails);
System.out.println("computer details are as below");
System.out.println("---------------------------");
System.out.println("prod No "+computer.getProductNo());
System.out.println("cost is "+computer.getCost());
System.out.println("purchase date is "+computer.getPurchaseDate());
System.out.println("computer parts details are as below");
System.out.println("------------------------------------");
System.out.println("Ram "+computer.getComputerDetails().getRamSize());
System.out.println("hard disk "+computer.getComputerDetails().getHardDiskSize());
System.out.println("processor speed "+computer.getComputerDetails().getProcessorSpeed());
System.out.println("-----end-------------");
}
}
Observation : if you look at the above class,it is creating ComputerDetails object(its dependency) by itself...this is normal java way of obtaining dependent objects.
Now look at the client program using spring dependency
package com.kb.spring.two.five;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ClientWithApplicationContext {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("com/kb/two/five/beans.xml");
Computer computer = context.getBean("computer",Computer.class);
System.out.println("computer details are as below");
System.out.println("---------------------------");
System.out.println("prod No "+computer.getProductNo());
System.out.println("cost is "+computer.getCost());
System.out.println("purchase date is "+computer.getPurchaseDate());
System.out.println("computer parts details are as below");
System.out.println("------------------------------------");
System.out.println("Ram "+computer.getComputerDetails().getRamSize());
System.out.println("hard disk "+computer.getComputerDetails().getHardDiskSize());
System.out.println("processor speed "+computer.getComputerDetails().getProcessorSpeed());
System.out.println("-----end-------------");
}
}
and beans.xml file to configure the beans
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5..xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<bean id="computer">
<property name= "productNo" value="123"></property>
<property name= "cost" value="37500.00"></property>
<property name= "purchaseDate" value="18/03/2013"></property>
<property name= "computerDetails" ref="computerDetails"></property>
</bean>
<bean id="computerDetails">
<property name="ramSize" value="2"></property>
<property name="hardDiskSize" value="500"></property>
<property name="processorSpeed" value="3.2"></property>
</bean>
</beans>
in beans.xml, we have added the beans definition, and we specified that Computer is dependent on ComputerDetails by using ref key word.
thas it, in the client program above , when we are getting Computer bean using context.getBean(), its dependent object ComputerDetails is created and initialized by the container and it injects it into the Computer object. this concept is called dependency injection in Spring.
Why it is called IOC ?
If the object having dependency is creating the dependent object then it is a straight way.
if someone else is creating your dependency then control is with that 3rd person(spring) hence it is called Inversion of control.

No comments:

Post a Comment