Pages

Tuesday, July 27, 2010

Spring - Dependency Injection- Setter Injection

The basic principle behind Dependency Injection (DI) is that objects define their dependencies . Then, it is the job of the container to actually inject those dependencies when it creates the bean.
The basic concept of the Inversion of Control pattern (also known as
Dependency Injection (DI) ) is that you do not create your objects but describe how they should be created. You don't directly connect your components and services together in code but describe which services are needed by which components in a configuration file. A container (in the case of the Spring framework, the IOC container) is then responsible for hooking it all up. In a typical IOC scenario, the container creates all the objects, wires them together by setting the necessary properties, and determines when methods will be invoked.

Two major flaovors of Dependency Injections are:
  1. Setter Injection (injection via JavaBean setters)
  2. Constructor Injection (injection via constructor arguments).
Setter Injection

As the name implies, using setter methods in a bean class the Spring IOC container will inject the dependencies. This technique is considered as the best approach for Dependency Injection. In this type of injection we have chance to reconfigure the bean instance as needed basis.

Employee.java

package com.ioc;

public class Employee {
private String name;
private Address address;
public Employee(){
}

public Address getAddress() {
return address;
}

public void setAddress(Address address) {
this.address = address;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}
Address.java

package com.ioc;
public class Address {
public Address(){

}
private String street;
private String city;

public String getCity() {
return city;
}

public void setCity(String city) {
this.city = city;
}

public String getStreet() {
return street;
}

public void setStreet(String street) {
this.street = street;
}

}


Test.java

package com.ioc;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;

public class Test{
public static void main(String args[]){
Resource xmlResource = new FileSystemResource("applicationContext.xml");
BeanFactory factory = new XmlBeanFactory(xmlResource);
Employee employee = (Employee)factory.getBean("employeeBean");
Address address = employee.getAddress();
System.out.println(employee.getName());
System.out.println(address.getCity());
System.out.println(address.getStreet());
}
}


applicationContext.xml

<?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"
<bean id="addressBean" class="com.ioc.Address">
<property name="street">
<value>Street</value>
</property>
<property name="city">
<value>Bangalore</value>
</property>
</bean>
<bean id="employeeBean" class="com.ioc.Employee">
<property name="empId" value="001"/>
<property name="address" ref="addressBean"/>
</bean>
</beans>

No comments: