Sunday 30 March 2014

Configuration Inheritance in Spring Beans

A bean definition can contain a lot of configuration information, including 
  • constructor arguments, 
  • property values, and 
  • container-specific information such as initialization method, static factory method name, and so on. 

A child bean definition inherits configuration data from a parent definition. The child definition can override some values, or add others, as needed.

Note that Spring Bean definition inheritance has nothing to do with Java class inheritance but inheritance concept is same.

Example

In our example, there is a World bean with two properties viz. message and language. Another bean India of which World is parent bean overrides message property and adds flag property. This is achieved by specifying the id of the parent bean in the child bean definition. The scenario is depicted with the help of following diagrams.
Spring Bean Configuration
Class Diagram
Note that there is no relationship between corresponding classes. 

The code is as follows.

spring-config.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

 <bean id="world" class="org.techmight.World">
  <property name = "message" value ="Hello World"/>
  <property name = "language" value ="all"/>
 </bean>
 
 <bean id="india" class="org.techmight.India" parent="world">
  <property name = "message" value ="Namaste India"/>
  <property name = "flag" value ="Tiranga"/>  
 </bean>
 
</beans>

World.java
package org.techmight;

public class World {

 private String message;
 private String language;

 public String getMessage() {
  return message;
 }

 public void setMessage(String message) {
  this.message = message;
 }

 public String getLanguage() {
  return language;
 }

 public void setLanguage(String language) {
  this.language = language;
 }
}

India.java
package org.techmight;

public class India {

 private String message;
 private String language;
 private String flag;

 public String getMessage() {
  return message;
 }

 public void setMessage(String message) {
  this.message = message;
 }

 public String getLanguage() {
  return language;
 }

 public void setLanguage(String language) {
  this.language = language;
 }

 public String getFlag() {
  return flag;
 }

 public void setFlag(String flag) {
  this.flag = flag;
 }
}

MainApp.java
package org.techmight;

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

public class MainApp {

 public static void main(String[] args) {
  AbstractApplicationContext context = new ClassPathXmlApplicationContext(
    "spring-config.xml");

  World world = (World) context.getBean("world");
  India india = (India) context.getBean("india");
  System.out.println("From World class - ");
  System.out.println("Message: " + world.getMessage());
  System.out.println("Language: " + world.getLanguage());

  System.out.println();
  System.out.println("From India class - ");
  System.out.println("Message: " + india.getMessage());
  System.out.println("Language: " + india.getLanguage());
  System.out.println("Flag: " + india.getFlag());
 }
}

Output
From World class -
Message: Hello World
Language: all

From India class -
Message: Namaste India (This value has overridden world bean value)
Language: all (This value is coming from world bean)
Flag: Tiranga (New property added to india bean)

No comments:

Post a Comment

Your comments are very much valuable for us. Thanks for giving your precious time.

Do you like this article?