Spring Bean Configuration


 

Spring Bean Configuration

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.

Spring 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"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

  <bean id="baseBean" class="bean.configuration.inheritance.Teacher">
    <property name="subject" value="Science" />
  </bean>

  <bean id="TeacherBean" parent="baseBean">
    <property name="student" ref="StudentBean" />
    <property name="teachername" value="Rakesh" />

  </bean>

  <bean id="StudentBean" class="bean.configuration.inheritance.Student">
    <property name="name" value="satya" />
    <property name="address" value="delhi" />

  </bean>
</beans>

When you run this application it will display message as shown below:


Teacher [Teacher Name=Rakesh Subject= Science, Student=Student [Student Name=satyaAddress=delhi]]

Download this example code

Ads