Tuesday, November 4, 2014

Spring Tutorial 10 – How To Use Inner Beans In Spring

As we have seen in earlier tutorial that we have created separate beans and whenever those bean required by other object, beans can be called by the id and ref pair tag. If there is no requirement of creating separate beans in the project then inner beans can be used which spring provides. We will see in this blogs that rather than giving id or reference tag for the separate bean, we can write the beans inside of a particular class and those beans cannot be used outside of the object. So this is one feature, spring provides.
            Let’s get into this tutorial by the following example of code.
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.springInnerBeanAliasExample “.

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

*******************************************************************************
package org.javaIsEasy.springInnerBeanAliasExample;

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 another java file in the same package with the name of “Area.java”.

In the Area file we will be creating object of the Rectangle class and another parameter with the name of ‘areaType’, 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.springInnerBeanAliasExample;

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 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.springInnerBeanAliasExample;

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/springInnerBeanAliasExample/Beans.xml");
           
            Area area=(Area)context.getBean("AreaBean");
            area.getAreaParameter();
           
}
}
*******************************************************************************
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.springInnerBeanAliasExample.Area">
                        <property name="smallArea">
                                    <bean class="org.javaIsEasy.springInnerBeanAliasExample.Rectangle">
                                                <property name="length" value="3" />
                                                <property name="width" value="2" />
                                    </bean>
                        </property>
                        <property name="mediumArea">
                                    <bean class="org.javaIsEasy.springInnerBeanAliasExample.Rectangle">
                                                <property name="length" value="6" />
                                                <property name="width" value="4" />
                                    </bean>
                        </property>
                        <property name="bigArea">

                                    <bean class="org.javaIsEasy.springInnerBeanAliasExample.Rectangle">
                                                <property name="length" value="8" />
                                                <property name="width" value="6" />
                                    </bean>
                        </property>
            </bean>

</beans>
*******************************************************************************
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