Home Tutorial Spring Spring3 Ioc Spring Bean Configuration


Share on Google+Share on Google+
Spring Bean Configuration
Posted on: August 31, 2010 at 12:00 AM
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.


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;

	public String toString() {
		return "Teacher [Teacher Name=" + teachername + " Subject= " + subject
				+ ", Student=" + student + "]";



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 + "]";


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");


<?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 id="TeacherBean" parent="baseBean">
    <property name="student" ref="StudentBean" />
    <property name="teachername" value="Rakesh" />


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


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


Related Tags for Spring Bean Configuration:

Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: August 31, 2010

Recommend the tutorial

Advertisements Advertisements