Home Tutorial Spring Spring3 Ioc Spring Override Bean

 
 

Share on Google+Share on Google+
Spring Override Bean
Posted on: August 31, 2010 at 12:00 AM
Advertisement
In this tutorial you will see an example of overriding in spring framework.

Spring Override Bean

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. The child class also override the inherited value.

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="subject" value="Biology" /> 

    <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= Biology, Student=Student [Student Name=satyaAddress=delhi]]

Download this example code

Advertisement

Related Tags for Spring Override Bean:


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
 

 

 

DMCA.com