Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

How to create an alias for a bean and inner bean in Spring Framework

In this article, we will learn about the bean alias and inner bean. Also we will see how we create bean alias and inner bean in spring.

Spring provides a way of dependency injection that is sometime also known as dependency inversion. Spring works as a factory of beans that produces the beans instances. To access these created bean instances, we are required to have a bean identity. Spring provides an alias to access these beans. Whenever a bean is being used for a single place, it can be defined as a inner bean instead of defining a separate bean.

Bean Alias and Inner Bean in Detail:

In Spring, Bean is accessed using id or name usually. Spring also provides an alias to bean. Hence we can also access a bean by bean alias name. To define a bean alias name, spring provides a <alias> tag as:

Syntax: <alias name = “bean-name” alias = “alias-name-for-defined-bean”/>

In the syntax, there is a <alias> tag that defines a “name” attribute, which represents the bean that is being aliased and attribute “alias” is used to specify the alias name for the bean.

Ex.: <alias name = “triangle” alias = “triangle_alias” />

In the example, we defined an alias for “triagle” bean. The “name” attribute refers to “triangle” bean defined and “alias” attribute defines an alias to “triangle” bean.

To import a bean into another bean, we use <idref> tag that reference to another bean defined in the configuration file. But, It is better to a bean as inner bean, if it is no longer usage beside one place. We define a inner bean as same as bean definition. But, The inner bean is defined inside the <property> tag as:

Syntax:

 <property name = “class-var-name”>
		<bean class = “class-name”>
			<property name = “class-var-name” value = “value”/>
			..
			..
		</bean>
</property>

The inner bean is the bean definition, defined inside the property tab of another bean.

Ex.:

<bean name = “Triangle”>
		<property name = “point1”>
			<bean class = “Point”>
				<property name = “x” value = “0”/>
				<property name = ”y” value = “0”/>
			</bean>
		</property>
	</bean>

The above example defines the “triangle” bean that defines an inner bean in itself by its property tag “point1”. The inner bean refers to “Point” class and defines values to its class variables “x” and “y”.

Let’s create a demo application using bean alias and inner bean.

Required Tools

To create this application, we are required following tools and applications:

  • Eclipse IDE (Ex. Eclipse Juno)
  • JDK Run Time Environment (Ex. JDK7)
  • Set of Spring Jar files

Required Jar files

We are required a set of Spring jar files as shown in figure 1:

Set of Spring 3.05 Jar Files

Figure 1: Set of Spring 3.05 Jar Files

The above figure shows a list of jar files provided by Spring 3.0.5 and one more jar file “commons-logging.jar” from Apache.

Project Structure

Project Structure Created in Eclipse IDE

Figure 2: Project Structure Created in Eclipse IDE

Figure 2 displays the project structure created in eclipse IDE for the demo application based on Spring 3.0.5 framework.

It contains three java class files and one spring configuration file. “TrianglePoint” is java class file that defines “Point” class type variables with its getter() and setter() methods. It also define draw() method that displays the values from “Point” class type variables as shown in Listing 1:

Listing 1: TrianglePoint.java

package spring;

public class TrianglePoint {
	private Point a;
	private Point b;
	private Point c;	
	
	public Point getA() {
		return a;
	}
	public void setA(Point a) {
		this.a = a;
	}
	public Point getB() {
		return b;
	}
	public void setB(Point b) {
		this.b = b;
	}
	public Point getC() {
		return c;
	}
	public void setC(Point c) {
		this.c = c;
	}
	
	public void draw(){
		System.out.println("Point A(" + getA().getX() + "," + getA().getY() + ")");
		System.out.println("Point A(" + getB().getX() + "," + getB().getY() + ")");
		System.out.println("Point A(" + getC().getX() + "," + getC().getY() + ")");
		System.out.println("\n\n");
	}
}

The above code defines three “Point” class type variable a, b and c with its getter() and setter() methods. It also define a draw() method that prints three triangle points into the console.

Now let’s define “Point.java” class file that defines two integer type variables with its getter() and setter() methods that will be used to represent three triangle points as shown in Listing 2:

Listing 2: Point.java

package spring;

public class Point {
	private int x, y;
	public int getX() {
		return x;
	}
	public void setX(int x) {
		this.x = x;
	}
	public int getY() {
		return y;
	}
	public void setY(int y) {
		this.y = y;
	}
}

The above code fines two int type variables x and y with its getter() and setter() methods.

Now, let’s define “Shape.java” class file as shown in Listing 3:

Listing 3: Shape.java

package spring;

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

public class Shape {
	public static void main(String[]args){
		ApplicationContext context = new ClassPathXmlApplicationContext("springContext.xml");
		TrianglePoint obj = (TrianglePoint)context.getBean("triangle-alias");
		obj.draw();
	}
}

The above code defines a “Shape” java class file. This class defines main() method that creates an instance of ApplicationContext interface type that is defined in “org.springframework.context” package. A reference of ClassPathXmlApplicationContext is passed using its parameterized constructor that accept spring configuration file name as string argument.

Now, To access the bean defined in spring configuration file “springContext.xml” file, call getBean() method of ApplicationContext class and type – cast this bean reference to “TrianglePoint” class type instance variable. The above line of code creates an object of “TrianglePoint” class.

Hence, We can call draw() method of “TrianglePoint” object “obj” now.

Now, let’s define a spring configuration xml file as listed in Listing 4:

Listing 4: springContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
	<bean id = "trianglePoint" class = "spring.TrianglePoint">
		<property name="a" ref="zero"/>
		<property name="b">
			<bean class = "spring.Point">
				<property name="x" value = "-20"/>
				<property name="y" value = "0"/>
			</bean>
		</property>
		<property name="c">
			<bean class = "spring.Point">					<!-- inner bean difinition. --> 
				<property name="x" value = "0"/>
				<property name="y" value = "20"/>
			</bean>
		</property>
	</bean>
	<bean id = "zero" class = "spring.Point">
		<property name="x" value = "0"/>
		<property name="y" value = "0"/>
	</bean>
	<alias name="trianglePoint" alias="triangle-alias"/>
</beans>

The above code defines a spring bean configuration file. This file defines xml version and encoding type in the first line of code using <?xml> tag. The next line defines the Document type to Spring data definition using <!DOCTYPE> tag. Now, Let’s define a “trianglePoint” bean using <bean> tag. An id attribute “trianglePoint” make this bean accessible by “trianglePoint” and class attribute refers this bean to “spring.TrianglePoint” class. This bean defines three property tag using <property> tag. These property tags refers to “TrianglePoint” class variables a, b and c using <property> tag attribute “name”. Property name “a” refers to another bean definition “zero" using its attribute “ref”. The next property name “b” defines a inner bean that refers to class “spring.Point”. This bean defines two property tags that refers to two points class variables “x” and “y” with its values “-20” and “0”. The last property name “c” also define an inner bean that also refers to “spring.Point” class and define two property name for its two class variables “x” and “y” with its values “0” and “20”. The bean name “zero” also refers to class “spring.Point” class and defines two properties for its two class variables “x” and “y” with its values “0” and “0”.

The last tag for this configuration file is tag that defines an alias to “trianglePoint” bean. A “name” attribute is used to define the bean name and “alias” attribute is used specify the “bean-alias-name”.

Once we have done with the configuration coding for the application. Now, Let’s execute this application. “Shape.java” class file has the main() method to execute this application as a desktop application. To run this application using Eclipse IDE, right click on the “Shape.java” class file and choose:

“Run As->Run As Java Application” as shown in figure 3:

 Run Java Application

Figure 3: Run Java Application

The above figure shows, how to run java application. Once all went fine, This application will produce three triangle points into the console as shown in figure 4:

Application Output

Figure 4: Application Output

Above figure shows three triangle points from spring configuration file. It reads values from spring configuration file.

Conclusion:

In this article, we learnt about the use of inner bean and alias for the beans. The inner bean saves the unusual pre-loading of the beans and creates the bean object, whenever it is required. The bean alias name gives us one more way to access our defined bean in the spring application.



Have good knowledge on Java, HTML, CSS and Android platform and is pursuing Masters in Computer Applications.

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login