Listing 4-76. Configuring Bean Inheritance
<bean id="inheritParent" class="com.apress.prospring3.ch4.inheritance.SimpleBean">
<property name="name">
<value>Clarence Ho</value>
</property>
<property name="age">
<value>22</value>
</property>
</bean>
<bean id="inheritChild" class="com.apress.prospring3.ch4.inheritance.SimpleBean"
parent="inheritParent">
<property name="age">
<value>35</value>
</property>
</bean>
In this code, you can see that the <bean> tag for the inheritChild bean has an extra attribute, parent,
which indicates that Spring should consider the inheritParent bean the parent of the bean. In case you
don't want a parent bean definition to become available for lookup from the ApplicationContext, you can
add the attribute abstract="true" in the <bean> tag when declaring the parent bean. Because the
inheritChild bean has its own value for the age property, Spring passes this value to the bean. However,
inheritChild has no value for the name property, so Spring uses the value given to the inheritParent
bean. Listing 4-77 shows the code for the SimpleBean class used in a previous configuration.
Listing 4-77. The SimpleBean Class
package com.apress.prospring3.ch4.inheritance;
import org.springframework.context.support.GenericXmlApplicationContext;
public class SimpleBean {
public String name;
public int age;
public static void main(String[] args) {
GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
ctx.load("classpath:app-context-xml.xml");
SimpleBean parent = (SimpleBean) ctx.getBean("inheritParent");
SimpleBean child = (SimpleBean) ctx.getBean("inheritChild");
System.out.println("Parent:\n" + parent);
System.out.println("Child:\n" + child);
}
public void setName(String name) {
this.name = name;
}
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home