Monday 1 May 2017

Spring: Bean definition Inheritance

In spring, a child bean can inherit configuration data from a parent bean. The child bean can override some values and some it can inherit.

Example
         <bean id="myParentBean" abstract="true" class="com.sample.pojo.ParentBean">
                 <property name="name" value="parentBean" />
                 <property name="version" value="16.10" />
         </bean>

         <bean id="child" class="com.sample.pojo.ChildBean" parent="myParentBean">
                 <property name="name" value="childBean" />
                 <property name="releasedDate" value="24th Nov 2016" />
                 <property name="author" value="Hari Krishna Gurram" />
         </bean>

As you see above snippet, bean ‘child’ inherits from myParentBean. childBean inherit the property value ‘version’ from myParentBean and overrides the property value ‘name’ to ‘childBean.

Find the complete working application.

ParentBean.java
package com.sample.pojo;

public class ParentBean {
 private String name;
 private String version;

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public String getVersion() {
  return version;
 }

 public void setVersion(String version) {
  this.version = version;
 }

 @Override
 public String toString() {
  StringBuilder builder = new StringBuilder();
  builder.append("ParentBean [name=").append(name).append(", version=").append(version).append("]");
  return builder.toString();
 }

}

ChildBean.java
package com.sample.pojo;

public class ChildBean {
 private String releasedDate;
 private String author;
 private String name;
 private String version;

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public String getVersion() {
  return version;
 }

 public void setVersion(String version) {
  this.version = version;
 }

 public String getReleasedDate() {
  return releasedDate;
 }

 public void setReleasedDate(String releasedDate) {
  this.releasedDate = releasedDate;
 }

 public String getAuthor() {
  return author;
 }

 public void setAuthor(String author) {
  this.author = author;
 }

 @Override
 public String toString() {
  StringBuilder builder = new StringBuilder();
  builder.append("ChildBean [releasedDate=").append(releasedDate).append(", author=").append(author)
    .append(", name=").append(name).append(", version=").append(version).append("]");
  return builder.toString();
 }

}

myConfiguration.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.xsd"
 default-init-method="init" default-destroy-method="cleanup">

 <bean id="myParentBean" class="com.sample.pojo.ParentBean">
  <property name="name" value="parentBean" />
  <property name="version" value="16.10" />
 </bean>

 <bean id="child" class="com.sample.pojo.ChildBean" parent="myParentBean">
  <property name="name" value="childBean" />
  <property name="releasedDate" value="24th Nov 2016" />
  <property name="author" value="Hari Krishna Gurram" />
 </bean>
</beans>

HelloWorld.java
package com.sample.test;

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

import com.sample.pojo.ChildBean;

public class HelloWorld {
 public static void main(String args[]) {
  ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "myConfiguration.xml" });

  ChildBean childBean = context.getBean("child", ChildBean.class);

  System.out.println(childBean);

  ((ClassPathXmlApplicationContext) context).close();
 }
}

Output
ChildBean [releasedDate=24th Nov 2016, author=Hari Krishna Gurram, name=childBean, version=16.10]


A child bean definition inherits scope, constructor argument values, property values, and method overrides from the parent, with the option to add new values. Any scope, initialization method, destroy method, and/or static factory method settings that you specify will override the corresponding parent settings.


If the parent definition does not specify a class, explicitly marking the parent bean definition as abstract is required, as follows.
 <bean id="myParentBean" abstract="true">
  <property name="name" value="parentBean" />
  <property name="version" value="16.10" />
 </bean>

 <bean id="child" class="com.sample.pojo.ChildBean" parent="myParentBean">
  <property name="name" value="childBean" />
  <property name="releasedDate" value="24th Nov 2016" />
  <property name="author" value="Hari Krishna Gurram" />
 </bean>


Previous                                                 Next                                                 Home

No comments:

Post a Comment