Tuesday, November 4, 2014

Spring Tutorial 11 – Use of alias and name in Beans In Spring

Alias is used when we need to have one name of a particular bean for the different use. Let’s take a scenario that a project has multiple module and we want to have same name of the bean for the different module. Let’s say a project has module A, B and C. The alias tag is used in bean when project needs loose coupling. In this scenario when A module is loaded with a particular name of alias of the bean, other B module and C module can also refer same alias name of the bean when they are loaded. Benefit of this is that we do not need to change the bean name with specific module. Same alias name is being used for all modules.
            If we want to change the name of the alias then we have to change only in one place and same name will be reflected for other modules as well just because of the loose coupling which spring provides.
Overall concept to use this is that if you want to use a particular name for a different purpose use alias.
Let’s get into alias details with following block 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

*******************************************************************************
[sourcecode language="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;
            }
           
           
}
[/sourcecode]
*******************************************************************************

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, 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-alias");
            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 create inner beans.

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>
<alias name="AreaBean" alias="areaBean-alias"/>
</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

*******************************************************************************
If you want to use name instead of alias you have change in the Beans.xml and in same way where it needs to be called also need changes.
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" name="areaNameBean">
             
                        <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>

*******************************************************************************
CallAreaApplication.java class also needs to be changed to call bean while having the name rather than id as mentioned below.

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("areaNameBean");
            area.getAreaParameter();
           
}
}
*******************************************************************************