In my previous post I have explained how to inherit collection values from the parent bean. Spring offers out of the box support on inheritance for the bean definitions in the XML configuration file. You can define any number of beans and extend them by using the parent attribute in the bean element. Child bean can inherit all the values, properties, methods, etc. from the parent beans. If you define the same method in the child bean, it simply overrides them in the child bean.
If parent bean have the class definition, then child bean need not define classes, it will use the parent bean. If you want to mention class name for the child bean, then it must have the same properties defined in the parent bean. Because it will try to search for the properties in the parent bean.
The parent bean is declared as abstract in the configuration file, it indicates that Spring container will not create any instance for that bean. If you try to access that bean, it will throw exception. If you don’t use a class type for the parent bean, then declaring it as abstract is mandatory.
Look at the below example. This is very simple example to demonstrate how to use the feature, you could try our more advanced examples and post your comments.
Person.java
package javabeat.net.core.ioc; public class Person { private String name; private String phone; private String email; private String city; private String country; public Person(){ System.out.println("Initializing bean..."); } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getCountry() { return country; } public void setCountry(String country) { this.country = country; } public String toString(){ StringBuffer buffer = new StringBuffer(); buffer.append("Name : " + this.name); buffer.append("Phone : " + this.phone); buffer.append("Email : " + this.email); buffer.append("City : " + this.city); buffer.append("Country : " + this.country); return buffer.toString(); } }
spring-beans-ioc.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd"> <context:component-scan base-package="javabeat.net.core.ioc"/> <bean id="personBean" class="javabeat.net.core.ioc.Person" abstract="true"> <property name="name" value="Vijay"/> <property name="phone" value="678567"/> <property name="email" value="[email protected]"/> <property name="city" value="Bangalore"/> <property name="country" value="India"/> </bean> <bean id="personBeanSub1" parent="personBean" > <property name="name" value="Muthu"/> <property name="phone" value="986756"/> <property name="email" value="[email protected]"/> </bean> <bean id="personBeanSub2" parent="personBean"> <property name="name" value="Hari"/> <property name="phone" value="452345"/> <property name="email" value="[email protected]"/> </bean> <bean id="personBeanSub3" parent="personBean"/> </beans>
SpringContextLoader.java
package javabeat.net.core.ioc; import org.springframework.context.support.ClassPathXmlApplicationContext; public class SpringContextLoader { public static void main (String args[]){ ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("javabeat/net/core/ioc/spring-beans-ioc.xml"); Person child1 = (Person)applicationContext.getBean("personBeanSub1"); Person child2 = (Person)applicationContext.getBean("personBeanSub2"); Person child3 = (Person)applicationContext.getBean("personBeanSub3"); System.out.println(child1.toString()); System.out.println(child2.toString()); System.out.println(child3.toString()); applicationContext.close(); } }
If you run the above example, the result will be:
Initializing bean... Initializing bean... Initializing bean... Name : MuthuPhone : 986756Email : [email protected] : BangaloreCountry : India Name : HariPhone : 452345Email : [email protected] : BangaloreCountry : India Name : VijayPhone : 678567Email : [email protected] : BangaloreCountry : India