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.

Syntax:


      
      



      
      

Example Explanation:

We have created two beans “HelloWorld” and “HelloJava”. HelloWorld.java have two properties msg1, msg2 and HelloJava.java 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.

Example:

HelloWorld.java

package com.w3schools360.business;

/**
 * This class will be used as a bean class.
 * @author W3schools360
 */
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;
	}	
}

HelloJava.java

package com.w3schools360.business;

/**
 * This class will be used as a bean class.
 * @author W3schools360
 */
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;
	}
	
}

applicationContext.java




   
       
       
   
   
    
       
       
    


Test.java

package com.w3schools360.business;

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

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());
 } 
}

Output:

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

 
Download this example.