Spring Tutorial - Spring Bean Inheritance








Spring supports bean configuration inheritance. We can define a bean and then further configure it to create new bean.

By using the bean inheritance we can share common values, properties or configurations.

A child bean inherits its parent bean configurations, properties and attributes.

In addition, the child beans can override the inherited value.

Java Bean

package com.java2s.common;
public class Customer {
    private int type;
    private String action;
    private String Country;
    public int getType() {
      return type;
    }//from   w ww.ja  v a  2  s.co  m
    public void setType(int type) {
      this.type = type;
    }
    public String getAction() {
      return action;
    }
    public void setAction(String action) {
      this.action = action;
    }
    public String getCountry() {
      return Country;
    }
    public void setCountry(String country) {
      Country = country;
    }
  @Override
  public String toString() {
    return "Customer [type=" + type + ", action=" + action + ", Country="
        + Country + "]";
  }
}




Bean configuration file

Here is the Bean configuration file

<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-2.5.xsd">
  <bean id="generalCustomer" class="com.java2s.common.Customer">
    <property name="country" value="USA" />
  </bean>
  <bean id="specialCustomer" parent="generalCustomer">
    <property name="action" value="backup" />
    <property name="type" value="1" />
  </bean>
</beans>

In the configuration file above 'generalCustomer' bean contains a 'USA' value for country property.

After the definition of 'generalCustomer' bean we defined the 'specialCustomer' bean inherited this value from its parent ('generalCustomer').

Here is the code to run this application.

package com.java2s.common;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App 
{
    public static void main( String[] args )
    {
      ApplicationContext context = 
      new ClassPathXmlApplicationContext("SpringBeans.xml");
      Customer cust = (Customer)context.getBean("specialCustomer");
      System.out.println(cust);
    }
}

The code above generates the following result.

Customer [type=1, action=backup, Country=USA]



Download Java2s_Spring_Bean_Inheritance.zip





abstract bean

In above example, we can still instantiate the 'generalCustomer' bean as follows.

Customer cust = (Customer)context.getBean("generalCustomer");

To make this base bean as a template and not allow Java code to instantiate it, add an 'abstract' attribute in the <bean> element.

<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-2.5.xsd">
  <bean id="generalCustomer" class="com.java2s.common.Customer" abstract="true">
    <property name="country" value="USA" />
  </bean>
  <bean id="specialCustomer" parent="generalCustomer">
    <property name="action" value="backup" />
    <property name="type" value="1" />
  </bean>
</beans>

After marking the 'generalCustomer' bean as abstract in the bean definition. It becomes a pure template for bean to inherit.

If we try to instantiate it, we will encounter the following error message.

Customer cust = (Customer)context.getBean("generalCustomer");
org.springframework.beans.factory.BeanIsAbstractException: 
  Error creating bean with name 'generalCustomer': 
  Bean definition is abstract

Bean Template

A parent bean doesn't need class attribute, it just need common properties for sharing.

Here's is an example

<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-2.5.xsd">
  <bean id="generalCustomer" abstract="true">
    <property name="country" value="USA" />
  </bean>
  <bean id="specialCustomer" parent="generalCustomer" 
      class="com.java2s.common.Customer">
    <property name="action" value="backup" />
    <property name="type" value="1" />
  </bean>
</beans>

In the xml code above the generalCustomer bean is a template which hosts common properties to other bean to inherit.

Overrride

We can override the inherited value by specify the new value in the child bean.

<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-2.5.xsd">
  <bean id="generalCustomer" class="com.java2s.common.Customer" abstract="true">
    <property name="country" value="USA" />
  </bean>
  <bean id="specialCustomer" parent="generalCustomer">
      <property name="country" value="Japan" />
    <property name="action" value="backup" />
    <property name="type" value="1" />
  </bean>
</beans>

The 'specialCustomer' bean overrides the parent 'generalCustomer' country property, from 'USA' to 'Japan'.

The code above generates the following result.



Download Java2s_Spring_Override.zip