Easy learning with example program codes

Spring bean definition inheritance

As we discussed earlier a bean definition in configuration metadata can contain constructor arguments, property values etc. Spring framework provides the facility that a child bean definition can inherits configuration data from a parent definition. A child definition can override some values of parent definition or add some others, as required.

Use parent attribute in child definition and pass parent bean into it.


<bean id="parentBeanId" class="TestParentBean">
      <property name="name1" value="value1"/>
      <property name="name2" value="value2"/>
<bean id="childBeanId" class="ChildBeanId" parent="parentBeanId">
      <property name="name1" value="value"/>
      <property name="name3" value="value3"/>

Example Explanation:

We have created two beans “HelloWorld” and “HelloJava”. have two properties msg1, msg2 and have three properties msg1, msg2, msg3. In applicationContext.xml HelloWorld bean defined with properties msg1 and msg2. The HelloJava bean use HelloWorld bean as parent and inherits the msg1 and msg2 properties. It use same value for msg1 property as we defined in HelloWorld and overrides the msg2 property. It also add one more property msg3.


 * This class will be used as a bean class.
 * @author codesjava
public class HelloWorld {
	private String msg1;
	private String msg2;
	public String getMsg1() {
		return msg1;
	public void setMsg1(String msg1) {
		this.msg1 = msg1;
	public String getMsg2() {
		return msg2;
	public void setMsg2(String msg2) {
		this.msg2 = msg2;

 * This class will be used as a bean class.
 * @author codesjava
public class HelloJava {
	private String msg1;
	private String msg2;
	private String msg3;
	public String getMsg1() {
		return msg1;
	public void setMsg1(String msg1) {
		this.msg1 = msg1;
	public String getMsg2() {
		return msg2;
	public void setMsg2(String msg2) {
		this.msg2 = msg2;
	public String getMsg3() {
		return msg3;
	public void setMsg3(String msg3) {
		this.msg3 = msg3;

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
   <bean id="helloWorld" class="">
       <property name="msg1" value="Java World"/>
       <property name="msg2" value="World."/>
    <bean id="helloJava" 
          class="" parent="helloWorld">
       <property name="msg2" value="Java"/>
       <property name="msg3" value="Java."/>

import org.springframework.context.ApplicationContext;
public class Test {
 public static void main(String[] args) {
  //Get ApplicationContext using spring configuration file.
  ApplicationContext context = 
	new ClassPathXmlApplicationContext("applicationContext.xml");
  //Get HelloWorld bean object from ApplicationContext instance. 
  HelloWorld helloWorld = (HelloWorld) context.getBean("helloWorld");
  //Process HelloWorld Object.
  System.out.println("HelloWorld bean properties: ");
  System.out.println("Hello " + helloWorld.getMsg1());
  System.out.println("Hello " + helloWorld.getMsg2());
  //Get HelloJava bean object from ApplicationContext instance.  
  HelloJava helloJava = (HelloJava) context.getBean("helloJava");
  //Process HelloJava Object.
  System.out.println("HelloJava bean properties: ");
  System.out.println("Hello " + helloJava.getMsg1());
  System.out.println("Hello " + helloJava.getMsg2());
  System.out.println("Hello " + helloJava.getMsg3());


HelloWorld bean properties: 
Hello Java World
Hello World.
HelloJava bean properties: 
Hello Java World
Hello Java
Hello Java.

Download this example.

Spring Tutorial

Spring framework.
Spring framework architecture.
Spring ioc container.
Spring bean.
Spring bean scopes.
Spring bean life cycle.
Spring callback methods.
Spring hello world.
Spring bean definition inheritance.
Spring bean definition template.
Spring dependency injection.
Spring constructor based injection.
Constructor injection type ambiguities.
Setter based dependency injection.
Spring dependency injection collections.
Spring autowire
Spring autowire by name
Spring autowire by type
Spring autowire by constructor
Spring JDBC tutorial
Spring JDBC Prepared Statement
Spring ResultSetExtractor
Spring RowMapper
Spring aop tutorial.
Spring AOP AspectJ Xml.
Spring AOP AspectJ Annotation.
Spring MVC tutorial.
Spring mvc framework.
Spring mvc configuration file.
Spring mvc hello world.
Spring MVC multiple controller.
Spring MVC login.
Spring mvc form handling.
Spring mvc exception handling.
Spring spel tutorial.
Spring spel hello world.
Spring spel operators.
Spring spel ternary operator.
Spring spel standardevaluationcontext.
Spring spel bean reference.
Spring spel method invocation.
Spring spel list, map.
Spring spel regex.
Maven Eclipse Spring
Spring boot overview
Spring boot architecture diagram
Spring boot components
Spring boot starter parent
Spring boot web app configuration
Run spring boot application
Spring boot change port
Spring boot change context path
Spring boot log sql statements
Spring boot hello world
Spring boot JSP
Spring boot thymeleaf
Spring boot with mysql
Spring security overview
Spring security architecture
Spring security maven dependency
Spring security xml
Spring security annotation
Spring security custom login xml
Spring security custom login annotation
Spring security form login
Spring security remember me
Spring security method level

Industrial Training

We offers Placement Oriented Training on Java, Spring, JSF, Hibernate, PHP, AngularJS, Angular 4, PLSQL, Oracle BI Publisher etc. We also provides Online training, please mail us at


We also provides the Development services for Website Development , Java Development, PHP Development, Android App Development etc. You can contact us on

Copyright © 2019 CodesJava Protection Status SiteMap Reference: Java Wiki