MrBool
You must be logged in to give feedback. Click here to login
[Close]

You must be logged to download.

Click here to login

[Close]

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

[Close]

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 use BeanDefinition Inheritance with Spring Framework

In this article, we will learn why we need to have the BeanDefinition Inheritance and the process to use BeanDefiniton Inheritance in Spring Framework.

[close]

You didn't like the quality of this content?

Would you like to comment what you didn't like?

If there are a lot of bean definition into our application and few of the bean are dependent or uses the same properties that are being defined repeatedly or need to be defined again and again. In order to save this configuration and code space, there is better way to use the inheritance functionality of the Object Oriented Paradigm. Using inheritance, we can inherit any pre-defined bean into another bean definition. In this way, we can have a separate bean that may contain all these common values and this bean can be inherited by other beans. So, Parent bean will be inherited by the child bean definition.

BeanDefiniton Inheritance in Detail:

As we have lot of beans definition in xml file and these beans have a common value to be defined, then bean definition can be handy. There may be multiple scenarios to use bean definition inheritance. We could apply bean definition inheritance with properties, list of properties and etc. We will have a look on the property and list of properties inheritance using an example.

To inherit a parent bean inside a child, we are required to define the “parent” attribute into the child bean definition. This attribute accepts the parent bean name as attribute value and inherits properties defined into the parent bean definition. These properties may be overridden, if required.

Listing 1: BeanDefiniton Inheritance Syntax

<bean id = “id-name” class = “class-name-with-package”> <!-parent bean definitionà
<properties ../>
</bean>

<bean id = “id-name” class = “class-name-with-package” parent = “parent-bean-name”>
 <!-child bean definitionà
	<properties ../>
</bean>

Listing 1 defines the BeanDefinition inheritance syntax, where parent and child beans are defined with their properties. Child bean inherits properties of parents, once parent attribute is defined into the child bean definition.

We should note that all the bean definition will be loaded at the time of spring bean application initialization, whether it is parent bean or the child bean. But, It should be defined the parent bean as a template, if it is not being used into the application for the bean class initialization.

To define a bean as a bean template using an attribute “abstract” to “true”. This attribute defines the spring container not to initialize the bean and use it as a template.

Listing 2: Define a bean as a template

<bean id = “id-name” class = “class-name-with-package” abstract = “true”>
	<properties ../>
</bean>

Listing 2 defines the syntax of the bean definition as a bean template. This bean definition uses an attribute “abstract” to “true”.

Now, let’s go with an example that will use the BeanDefinition inheritance to inherit properties. This example will uses the point and triangle bean classes and displays three triangle points into the web page.

Let’s define the java bean class “Points.java” in Listing 3:

Listing 3: Points.java

package com.springmvcapp.controller;

public class Points {
	private int x;
	private int 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;
	}
	
}

Listing 3 defines the Java bean class “Points.java” that defines two class member variables “x” and “y” of integer type with its getter() and setter() methods. These variables will be used to draw triangle points. These points are defined into the “Triangle.java” java bean class using three “Points” class instances as defined into Listing 4:

Listing 4: Triangle.java

package com.springmvcapp.controller;

import java.awt.Point;

public class Triangle{
	private Points a;
	private Points b;
	private Points c;

	public Points getA() {
		return a;
	}


	public void setA(Points a) {
		this.a = a;
	}


	public Points getB() {
		return b;
	}


	public void setB(Points b) {
		this.b = b;
	}


	public Points getC() {
		return c;
	}


	public void setC(Points c) {
		this.c = c;
	}


	public String draw(){
		String string = "";
	
		string += "PointA (" + getA().getX() + "," + getA().getY() + ")\n";
		string += "PointB (" + getB().getX() + "," + getB().getY() + ")\n";
		string += "PointC (" + getC().getX() + "," + getC().getY() + ")\n";
		
		return string;	
	}
}

Listing 4 defines the java bean class “Triangle.java” that declares three “Points” class instances “a”, “b” and “c” with its getter() and setter() methods. It also defines draw() method that returns String type data to display three triangle points.

To configure their bean definition using BeanDefinition Inheritance strategy that inherits property of point “a” into another bean definition “triangle”, “spring.xml” configuration file is defined into Listing 5:

Listing 5: spring.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"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    <bean id = "triangleParent" class = "com.springmvcapp.controller.Triangle" abstract = “true”>
    	<property name="a" ref="a"/>
    </bean>
    
    <bean id = "triangle" class = "com.springmvcapp.controller.Triangle"  parent = "triangleParent">
    	<property name = "b" ref = "b"/>
    	<property name = "c" ref = "c"/>	
    </bean>
    
    <bean id = "a" class = "com.springmvcapp.controller.Points">
    	<property name="x" value="0"/>
    	<property name="y" value="0"/>
    </bean>
    
    <bean id = "b" class = "com.springmvcapp.controller.Points">
    	<property name="x" value="0"/>
    	<property name="y" value="-20"/>
    </bean>
    
    <bean id = "c" class = "com.springmvcapp.controller.Points">
    	<property name="x" value="0"/>
    	<property name="y" value="20"/>
    </bean>
</beans>

Listing 5 defines set of beans definition, where “parentTriangle” is the abstract bean of the bean template that will be inherited by other child bean definitions. This bean defines a single property to be inherited by their children.

Child bean “triangle” defines two more properties references to property “b” and “c” and inherits property “a” using bean definition “parentTriangle”. The “parentTriangle” also references the property “a” to bean definition “a”.

We can also inherit the collection type properties as defined into Listing 6 using “spring.xml” configuration file:

Listing 6: spring.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"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    <bean id = "triangleParent" class = "com.springmvcapp.controller.Triangle">
    	<property name="points" ref="a">
    		<list>
    			<ref bean="a"/>
    		</list>
    	</property>
    </bean>
    
    <bean id = "triangle" class = "com.springmvcapp.controller.Triangle"  parent = "triangleParent">
    	<property name="points">
    		<list merge="true">
    			<ref bean="b"/>
    			<ref bean="c"/>
    		</list>
    	</property>
    </bean>
    
    <bean id = "a" class = "com.springmvcapp.controller.Points">
    	<property name="x" value="0"/>
    	<property name="y" value="0"/>
    </bean>
    
    <bean id = "b" class = "com.springmvcapp.controller.Points">
    	<property name="x" value="0"/>
    	<property name="y" value="-20"/>
    </bean>
    
    <bean id = "c" class = "com.springmvcapp.controller.Points">
    	<property name="x" value="0"/>
    	<property name="y" value="20"/>
    </bean>
</beans>

Listing 6 defines the spring configuration into “spring.xml” file that configures triangle three points using collection tag <list>. The attribute “merge” is set to “true” to merge properties into <list>, while inheriting using child bean definition. In order to use list collection configuration, we are required to set up our “triangle” bean definition using List collection type variable “points” as defined into Listing 7:

Listing 7: Triangle.java

package com.springmvcapp.controller;

import java.util.List;

public class Triangle{
	private List<Points> points;

	

	public List<Points> getPoints() {
		return points;
	}



	public void setPoints(List<Points> points) {
		this.points = points;
	}



	public String draw(){
		String string = "";
		
		for(Points point : points){
			string += "Point (" +  point.getX() + "," + point.getY() + ")/n";
		}
		
		return string;	
	}
}

Listing 7 defines the java bean class “Triangle.java” using List type collection to display three triangle points. It defines the class member variable “points” of List type with its getter() and setter() methods.

Now let’s define a main class that will access these bean class instances using spring mechanism defined in Listing 8:

Listing 8: HelloWorld.java

package com.springmvcapp.controller;

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

public class HelloWorld {
	public static void main(String[]args){
		   
	      ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
    	  Triangle obj = (Triangle) context.getBean("triangle");
    	  System.out.println(obj.draw());
    	
      }
	 
}

Listing 8 defines the main() method that creates an instance of ApplicationContext class and initializes the spring bean container using spring.xml configuration file and initializes Triangle bean class and call the draw() method and displays the three triangle points into the console as shown in Figure 1:

Three triangle points

Figure 1: Three triangle points

Required Tools and Application:

To create and run this application, we are required to have following list of tools and applications:

  • Eclipse IDE (Eclipse Juno)
  • JDK Run time environment (JDK 7)
  • Set of spring jar files (Spring 2.5) as shown into Figure 2:
Set of spring jar files

Figure 2: Set of spring jar files

Figure 2 displays set of spring jar files of version 2.5.

Directory Structure Created:

shows the directory structure created by Eclipse IDE:

Figure 3 shows the directory structure created by Eclipse IDE:

Conclusion:

In this article, we learn about the need of having BeanDefinition Interface into the spring application. We also came to know about the ways of implementation of BeanDefiniton interface into spring application.



I am a software developer from India with hands on experience on java, html for over 5 years.

What did you think of this post?
Services
Know how to keep MrBool Online
SUPPORT US
SUPPORT US
With your help, we can keep providing free content and helping you to be a better professional
support us
[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