In this tutorial you will learn about spring bean configuration and also see inheritance in bean configuration.
In this tutorial you will learn about spring bean configuration and also see inheritance in bean configuration.The support of inheritance is present in the Spring framework and common values or configuration is shared among beans. The child bean inherits the properties and configuration of the parent bean or base bean.
Teacher.java
package bean.configuration.inheritance; public class Teacher { private Student student; private String teachername; private String subject; public String getSubject() { return subject; } public void setSubject(String subject) { this.subject = subject; } public Student getStudent() { return student; } public void setStudent(Student student) { this.student = student; } public String getTeachername() { return teachername; } public void setTeachername(String teachername) { this.teachername = teachername; } @Override public String toString() { return "Teacher [Teacher Name=" + teachername + " Subject= " + subject + ", Student=" + student + "]"; } }
Student.java
package bean.configuration.inheritance; public class Student { private String name; private String address; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String toString() { return "Student [Student Name=" + name + "Address=" + address + "]"; } }
AppMain.java
package bean.configuration.inheritance; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class AppMain { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext( new String[] { "context.xml" }); Teacher cust = (Teacher) context.getBean("TeacherBean"); System.out.println(cust); } }
context.xml
<?xml version="1.0" encoding="UTF-8"?>
|
Teacher [Teacher Name=Rakesh Subject= Science, Student=Student [Student Name=satyaAddress=delhi]] |