Online Hibernate Training just in Rs. 1000

One week online training in Hibernate ORM framework. Learn Hibernate Framework and enhance your skills. Best Online Hibernate training at 90% discount. Join Now!! Offer is valid Only for limited Period!!! Online Training

Inheritance in Spring

Inheritance Demo, In the example given below we are going to tell about the inheritance in the Spring framework.


Spring 4 Introduction and example Features of Spring Framework 5 How to make Spring web Login form? How to make Simple form in Spring web? Spring, Hibernate login and registration application Spring Framework Tutorial for beginners with examples Spring Framework for Apache Hadoop 2.3.0 GA released Spring Framework 4.1 GA is released with major features Why to use Spring Framework? Spring Framework 4.1 - First Release candidate available Spring IO Platform 1.0.0 Released Spring 4: Login Form using Spring MVC and Hibernate Example Spring 4 MVC Login form Example with source code Spring 4 MVC Hello World Example: Spring 4 MVC Tutorial will full source code Spring Web MVC Application Error:ClassNotFoundException: DispatcherServlet on deploying Features of Spring 4 Spring Framework 4.0 released Spring Framework 4: Spring Framework 4 Tutorials and Example Spring Integration 3.0 Release is released and available for download Spring Tutorial for Beginners Java Springs Framework Tutorial Spring Architecture Spring Framework Tutorials database spring registration form Spring Login Example Roseindia Spring Tutorial Spring Tutorial Spring 3.2 MVC insert and retrieve blob from the database The hidden tag The errors tag net.roseindia.dao net.roseindia.service net.roseindia.model net.roseindia.controller Spring 3.2 MVC Hibernate Example Spring 3.2 MVC, Upload File in a specific folder Spring 3.2 MVC Form Handling The textarea tag The options tag The option tag The select tag The radiobuttons tag The radiobutton tag The checkboxes tag mvc:default-servlet-handler Static Resources Configuration View Controllers Configuration View resolvers Spring 3.2 MVC Hello World Example Content Negotiation Configuration

Inheritance in Spring


Inheritance Demo, In the example given below we are going to tell about the inheritance in the Spring framework. By inheritance we mean a way of forming new classes using classes that have already been defined. Here we have created a simple bean and used this bean as a template for creating other beans.

<bean id="parent" class="mybean" >:-Creates a  Parent bean which would be used as a template for creating other beans. Here "mybean" is the name of the bean class which would be referred in the xml file with the id "parent".

<bean id="child" class="mybean" parent="parent">:-Creates a child bean which would be inheriting from the parent bean defined above.

parent="parent":-Specify that this bean is inheriting the properties of some other bean. 

<bean id="parent" class="mybean" >   <property name="name" value=""/>   
  <bean id="child" class="mybean" parent="parent">   <property name="address" value="Rohini"/>   


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""

  <bean id="parent" class="mybean" >
  <property name="name" value=""/>

  <bean id="child" class="mybean" parent="parent">
  <property name="address" value="Rohini"/>

  <bean id="subchild" class="mybean" parent="parent"/>


This is the file through which we are retrieving the property of the bean defined above. Some of the methods which are used here are as follows:-

XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource("context.xml")):-This method creates an instance of the XmlBeanFactory which is used to read bean definition from an XML document

new ClassPathResource("context.xml"):-
Creates a new ClassPathResource for ClassLoader .Here the context.xml is the file which is to be loaded.

(bf.getBean("child")):-This method returns an instance of the bean named "child", which is shared or independent, of the given bean name.

(bf.getBean("subchild")):-This method returns an instance of the bean named "subchild", which is shared or independent, of the given bean name.

stringBuilder.append("Bean"):-This is the method of the class string Builder which appends the specified string to this character sequence.

import org.springframework.beans.factory.xml.XmlBeanFactory;
public class Main {
  public static void main(String[] argsthrows Exception {
  XmlBeanFactory bf = new XmlBeanFactory(new ClassPathResource("context.xml"));
  System.out.println("===============Inheritance demo=================");
class mybean {
  private String name;
  private String address;
  public void setName(String name) { = name;
  public void setAddress(String address) {
  this.address = address;
  public String toString() {
  final StringBuilder stringBuilder = new StringBuilder();
  stringBuilder.append(", address=").append(address);
  return stringBuilder.toString();

Output of the program

Nov 25, 2008 3:39:29 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions INFO: Loading XML bean definitions from class path resource [context.xml] ===============Inheritance demo================= Bean{name='', address=Rohini} Bean{name='', address=null} BUILD SUCCESSFUL (total time: 1 second)

Download source code


Share on Google+Share on Google+

Inheritance in Spring

Posted on: May 22, 2009 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: Inheritance in Spring   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
Steve White
April 17, 2011
libraries for building and running

build: /usr/share/java/spring-core.jar:/usr/share/java/spring-beans.jar run: .:/usr/share/java/spring-core.jar:/usr/share/java/spring-beans.jar:/usr/share/java/commons-logging.jar
September 27, 2011
Can't use extend?

Does spring inheritance require xml config to define a subclass? Is it not possible to use standard java inheritance: public class child extends parent {...