Monday, February 7, 2011

Spring IOC


Spring concepts
1.        IOC
a.        Dependency Injection.
                                                               i.      Setter Injection.
                                                              ii.      Constructor Injection             

b.        Auto Wiring.
                                                               i.      No
                                                              ii.      byname
                                                            iii.      byType.
                                                            iv.      Constructor
                                                              v.      autodetect
2.        Controller
               
Dependency Injection:
                                                This article presents how to write the Constructor Injection in Spring IOC. There is two types of Dependency Injection(DI) techniques we can use.
1)      Setter Injection .
2)       Constructor Injection

Setter Injection:

As the name implies, using constructor the Spring IOC container will inject the dependencies. The constructor will take arguments based on number of dependencies required. This is one of the drawback using Constructor based DI. You don't have option to reconfigure the dependencies at later point of time, since all the dependencies are resolved only at the time of invoking the constructor. if you don't have requirement to inject all the dependencies, please use Setter Injection technique to obtain the more flexibilty on configuring beans.

   <constructor-arg index="0" type="java.lang.String" value="MyName"/>

The above code is an example for how to pass dependency arguments to the constructor. Index attribute is useful when you have multiple arguments with the same type, you can inform the IOC container about the order of the arguments. You can pass bean reference using the ref element.
Employee.java
package javabeat.net.spring.ioc;

/**
 * Source : http://www.javabeat.net
 */

public class Employee {
    private String name;
    private String empId;
    private Address address;
    public Employee(String name, String empId, Address address){
        this.name = name;
        this.empId = empId;
        this.address = address;
    }

    public Address getAddress() {
        return address;
    }

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

    public String getEmpId() {
        return empId;
    }

    public void setEmpId(String empId) {
        this.empId = empId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }   
}
Address.java
package javabeat.net.spring.ioc;
/**
 * Source : http://www.javabeat.net
 */

public class Address {
    public Address(){

    }
    private String street;
    private String city;
    private String pincode;

    public String getCity() {
        return city;
    }

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

    public String getPincode() {
        return pincode;
    }

    public void setPincode(String pincode) {
        this.pincode = pincode;
    }

    public String getStreet() {
        return street;
    }

    public void setStreet(String street) {
        this.street = street;
    }
}
ConstructorInjection.java
package javabeat.net.spring.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;

/**
 * source : www.javabeat.net
 */
public class ConstructorInjection {
    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(employee.getEmpId());
        System.out.println(address.getCity());
        System.out.println(address.getStreet());
        System.out.println(address.getPincode());       
    }}

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"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
    <bean id="addressBean" class="javabeat.net.spring.ioc.Address">
        <property name="street">
            <value>Street</value>
        </property>
        <property name="city">
            <value>Bangalore</value>
        </property>
        <property name="pincode">
            <value>567456</value>
        </property>
    </bean>
    <bean id="employeeBean" class="javabeat.net.spring.ioc.Employee">
        <constructor-arg index="0" type="java.lang.String" value="MyName"/>
        <constructor-arg index="1" type="java.lang.String" value="001"/>
        <constructor-arg index="2">
            <ref bean="addressBean"/>
        </constructor-arg>
    </bean>
</beans>








Constructor 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 javabeat.net.spring.ioc;

/**
 *
Source : http://www.javabeat.net
 */

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

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

    public String getEmpId() {
        return empId;
    }

    public void setEmpId(String empId) {
        this.empId = empId;
    }

    public String getName() {
        return name;
    }

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


Address.java

package javabeat.net.spring.ioc;
/**
 * Source : http://www.javabeat.net
 */

public class Address {
    public Address(){

    }
    private String street;
    private String city;
    private String pincode;

    public String getCity() {
        return city;
    }

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

    public String getPincode() {
        return pincode;
    }

    public void setPincode(String pincode) {
        this.pincode = pincode;
    }

    public String getStreet() {
        return street;
    }

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

}




ConstructorInjection.java

package javabeat.net.spring.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;

/**
 * source : www.javabeat.net
 */
public class ConstructorInjection {
    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(employee.getEmpId());
        System.out.println(address.getCity());
        System.out.println(address.getStreet());
        System.out.println(address.getPincode());       
    }}

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"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
    <bean id="addressBean" class="javabeat.net.spring.ioc.Address">
        <property name="street">
            <value>Street</value>
        </property>
        <property name="city">
            <value>Bangalore</value>
        </property>
        <property name="pincode">
            <value>567456</value>
        </property>
    </bean>
    <bean id="employeeBean" class="javabeat.net.spring.ioc.Employee">
        <property name="name" value="MyName"/>
        <property name="empId"  value="001"/>
        <property name="address" ref="addressBean"/>
    </bean>
</beans>


Note: 
The Following Jar files are needed to run the Above Code:
1.       commons-logging-1.0.4.jar
2.       org.springframework.beans-3.0.1.jar
3.       org.springframework.core-3.0.1.jar