Inheritance mapping

Inheritance is a major pillar of Java, and we understand how important this is. Spring supports for bean definitions to configure in XML. Inheritance support is provided by the parent attribute, which says missing properties can be used from the parent bean. Using parent gives similarity with the parent class in inheritance when we develop Java code. It's even possible to override the properties from the parent bean. Let's find out how to use it practically with the help of the following steps:

  1. Create Ch02_Inheritance as a Java project.
  2. Add the jars.
  3. Create a Student class in the com.ch02.beans package with the following code:
public class Student { 
  private int rollNo; 
  private String name; 
  // getters and setters 
} 
  1. Create an EnggStudent class inherited from Student, as follows:
public class EnggStudent extends Student { 
  private String branch_code; 
  private String college_code; 
  // getters and setters 
} 
  1. Create student.xml in classpath to configure the bean for student, as shown in the following lines of code:
<bean id="student" class="com.ch02.beans.Student"> 
  <property name="rollNo" value="34"/> 
  <property name="name" value="Sasha"/> 
</bean> 
  1. Now, it's time to configure the bean for EnggStudent. First of all, an ordinary configuration that we don't want to use, is like this:
<bean id="engg_old" class="com.ch02.beans.EnggStudent"> 
  <property name="rollNo" value="34"/> 
  <property name="name" value="Sasha"/> 
  <property name="branch_code" value="Comp230"/> 
  <property name="college_code" value="Clg_Eng_045"/> 
</bean> 

It's very clear that we repeated the configuration for the properties rollNo and name. We don't have to repeat the configuration by configuring the parent attribute, as shown in the following piece of code:

<bean id="engg_new" class="com.ch02.beans.EnggStudent" 
  parent="student"> 
  <property name="branch_code" value="Comp230"/> 
  <property name="college_code" value="Clg_Eng_045"/> 
</bean> 

Even though we skip configuring name and rollNo, and reuse it from the student bean, it's also possible to override any one of them, as shown in the following lines of code:

<bean id="engg_new1"class="com.ch02.beans.EnggStudent" 
  parent="student"> 
  <property name="rollNo" value="40"/> 
  <property name="branch_code" value="Comp230"/> 
  <property 
    name="college_code" value="Clg_Eng_045"/> 
</bean> 

The choice is yours, which one to use!!

  1. Write TestStudent with the main function as follows:
public static void main(String[] args) { 
  // TODO Auto-generated method stub 
  ApplicationContext context=new 
    ClassPathXmlApplicationContext("student.xml"); 
  //old configuration 
  EnggStudentstudent_old= 
    (EnggStudent)context.getBean("engg_old"); 
  System.out.println("with old configuration"); 
  System.out.println(student_old.getName()+
    "	"+student_old.getRollNo()+"	"+ 
  student_old.getCollege_code()); 
  //new configuration 
  EnggStudentstudent_new= 
    (EnggStudent)context.getBean("engg_new"); 
  System.out.println("with new configuration"); 
  System.out.println(student_new.getName()+ 
    "	"+student_new.getRollNo()+"	"+ 
    student_new.getCollege_code()); 
  System.out.println("with new configuration with
    overridding roll number"); 
  //new configuration with overriding the roll number 
  EnggStudentstudent_new1= 
    (EnggStudent)context.getBean("engg_new1"); 
  System.out.println(student_new1.getName()+"	"+ 
    student_new1.getRollNo()+"	"+ 
    student_new1.getCollege_code()); 
} 
  1. The following screenshot shows the console output:

It's clear from the output that the developers are able to obtain a Student instance as well as EnggStudent.

  1. In some cases, we don't want anybody to use a Student instance, or nobody should be able to get a Student instance. In such situations, configure the abstract attribute on the bean whose instance developers don't want to make publically available. By default, the value of abstract="false" states that anybody can obtain an instance of the bean. We will configure abstract="true", making it unavailable. The updated configuration of the Student class will be as follows:
<bean id="student" class="com.ch02.beans.Student" 
      abstract="true"> 
<property name="rollNo" value="34"/> 
<property name="name" value="Sasha"/> 
</bean> 
  1. Now, whenever someone asks for the student bean, BeanIsAbstractException will be thrown. You can try out the code by adding the following lines in TestStudent:
System.out.println("obtaining Student instance"); 
Student student=(Student)context.getBean("student"); 
  1. On execution, we will get the following stack trace, which specifies that a bean creation exception occurred while obtaining the Student bean:
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.189.23