Tuesday, November 4, 2014

Spring tutorial 09 - Injecting Multiple Objects in Spring

We have already got some information regarding injection (by constructor and setter). In this tutorial we will get to know that how multiple bean objects at the run time.

Let's understand this with an example.

First 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.springInjectionObjectsExample".

Now create a java file with the name of "Rectangle.java" in this package.

Rectangle.java

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

package org.javaIsEasy.springInjectionObjectsExample;

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;
 }


}

*******************************************************************************
Create an other java file in the same package with the name of "Area.java".

In the Area file we will creating object of the Rectangle class, we will give the reference of the Rectangle class in this Area class. So let's see how it goes.

Area.java
*******************************************************************************
package org.javaIsEasy.springInjectionObjectsExample;

public class Area {

 private Rectangle smallArea;
 private Rectangle mediumArea;
 private Rectangle bigArea;





 public Rectangle getSmallArea() {
  return smallArea;
 }

 public void setSmallArea(Rectangle smallArea) {
  this.smallArea = smallArea;
 }
 public Rectangle getMediumArea() {
  return mediumArea;
 }

 public void setMediumArea(Rectangle mediumArea) {
  this.mediumArea = mediumArea;
 }
 public Rectangle getBigArea() {
  return bigArea;
 }

 public void setBigArea(Rectangle bigArea) {
  this.bigArea = bigArea;
 }


 public void getAreaParameter()
 {
  System.out.println("smallArea length-->"+getSmallArea().getLength()+", width-->"+getSmallArea().getWidth());
  System.out.println("mediumArea length-->"+getMediumArea().getLength()+", width-->"+getMediumArea().getWidth());
  System.out.println("bigArea length-->"+getBigArea().getLength()+", width-->"+getBigArea().getWidth());
 }


}

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

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

Here the Beans.xml file is used to define spring bean configuration. The following code shows how to set a property value through constructor injection.

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.springInjectionObjectsExample.Area">
      <property name="smallArea" ref="smallArea"/>
      <property name="mediumArea" ref="mediumArea"/>
      <property name="bigArea" ref="bigArea"/>
     
   </bean>

 <bean id="smallArea" class="org.javaIsEasy.springInjectionObjectsExample.Rectangle">
      <property name="length" value="3"/>
      <property name="width" value="2"/>
   </bean>
   
   <bean id="mediumArea" class="org.javaIsEasy.springInjectionObjectsExample.Rectangle">
      <property name="length" value="6"/>
      <property name="width" value="4"/>
   </bean>
   
   <bean id="bigArea" class="org.javaIsEasy.springInjectionObjectsExample.Rectangle">
      <property name="length" value="8"/>
      <property name="width" value="6"/>
   </bean>
   
</beans>

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

Now to call this we have to create another class which will have main method.

Now create another java file with the name of "CallAreaApplication.java" in the same package.
This class has the main method, configuration file will be loaded in this class and let's see how bean is called here.

CallAreaApplication.java
*******************************************************************************
package org.javaIsEasy.springInjectionObjectsExample;

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/springInjectionObjectsExample/Beans.xml");

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

 }
}

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

Structure of the project :




Let's run this program to get the result:

Output:

*******************************************************************************
smallArea length-->3, width-->2
mediumArea length-->6, width-->4
bigArea length-->8, width-->6

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

No comments:

Post a Comment