Friday, April 4, 2014

Spring tutorial 06 - Setter Injection in the Spring

Setter Injection :

       Spring IOC support the setter injection, which is normally used in the projects. 'set' method is used in the class file to garner property name that are configurable in the config file.

       From a configuration standpoint, setter injection is easier to read because the property name being set is assigned as an attribute to the bean, along with the value being injected. Setter-based DI is accomplished by the container calling setter methods on your beans after invoking a no-argument constructor or no-argument static factory method to instantiate your bean.

See the example below for basic setter injection on our class.

Let's create a project say "SpringTutorial" now add the required jar support to spring, to add the jar  you can follow the Spring Tutorial 02 -My First 'Hello World' Program in Spring blog.


Now create a package with the name of "org.javaIsEasy.springSetterInjectionExample".
Create a java file in this package with the name of "Area.java".

Area.java

*******************************************************************************
package org.javaIsEasy.springSetterInjectionExample;

public class Area {

 private String areaType;
 private Rectangle rectangle;

 public String getAreaType() {
  return areaType;
 }

 public void setAreaType(String areaType) {
  this.areaType = areaType;
 }

 public Rectangle getRectangle() {
  return rectangle;
 }

 public void setRectangle(Rectangle rectangle) {
  this.rectangle = rectangle;
 }


}

*******************************************************************************

Now create another java file with the name of "Rectangle.java" in the same package.

Rectangle.java
*******************************************************************************
package org.javaIsEasy.springSetterInjectionExample;

public class Rectangle {

 private int length;
 private int width;

 public int getLength() {
  return length;
 }
 public void setLength(int length) {
  this.length = length;
 }
 public int getWidth() {
  return width;
 }
 public void setWidth(int width) {
  this.width = width;
 }


}

*******************************************************************************

Now create another java file with the name of "CallAreaApplication.java" in the same package.

CallAreaApplication.java
*******************************************************************************

package org.javaIsEasy.springSetterInjectionExample;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class CallAreaApplication {
public static void main(String[] args) {

 ApplicationContext context = new ClassPathXmlApplicationContext("org/javaIsEasy/springSetterInjectionExample/Beans.xml");

 Area area=(Area)context.getBean("AreaBean");

 int length=area.getRectangle().getLength();
 int width=area.getRectangle().getWidth();
 String areaType=area.getAreaType();

 System.out.println("Area Name : "+areaType+"\nLength : "+length+"\nWidth : "+width+"\nArea Value : "+length*width);
}
}

*******************************************************************************

Now we have create bean configuration file with the name of "Beans.xml" in the same package. 


Beans.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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

   <bean id="AreaBean" class="org.javaIsEasy.springSetterInjectionExample.Area">
      <property name="areaType" value="Rectangle"/>
      <property name="rectangle" ref="RectangleBean"/>
   </bean>

 <bean id="RectangleBean" class="org.javaIsEasy.springSetterInjectionExample.Rectangle">
      <property name="length" value="6"/>
      <property name="width" value="4"/>
   </bean>
   
</beans>
*******************************************************************************

Structure of the project :




Lets run this program to get the result:

Output:

*******************************************************************************
Area Name : Rectangle
Length : 6
Width : 4
Area Value : 24


*******************************************************************************