[size=large]In Java, children classes share a common base type that is determined by their
parent bean. This means that there’s no way in Java for children classes to extend
a common type to inherit properties and/or methods but not to inherit the common
parent.
In Spring, however, sub-beans do not have to share a common parent type.
Two <bean>s with completely different values in their class attributes can still
share a common set of properties that are inherited from a parent bean.
Consider the following abstract parent <bean> declaration:
<bean id="basePerformer" abstract="true">
<property name="song" value="Somewhere Over the Rainbow" />
</bean>
instrument=saxophone
song="Jingle Bells"
baseSaxophonist:Instrumentalist
kenny david
song="Mary had a..."
frank
The frank bean overrides
the song property
Figure 3.3 The frank bean inherits from the baseSaxophonist bean, but
overrides the song property.
78 CHAPTER 3
Advanced bean wiring
This basePerformer bean declares the common song property that our two performers
will share. But notice that it doesn’t have a class attribute set. That’s
okay, because each child bean will determine its own type with its own class
attribute. The new declarations of taylor and stevie are as follows:
<bean id="taylor"
class="com.springinaction.springidol.Vocalist"
parent="basePerformer" />
<bean id="stevie"
class="com.springinaction.springidol.Instrumentalist"
parent="basePerformer">
<property name="instrument" ref="guitar" />
</bean>
Notice that these beans use both the class attribute and the parent attribute
alongside each other.[/size]
parent bean. This means that there’s no way in Java for children classes to extend
a common type to inherit properties and/or methods but not to inherit the common
parent.
In Spring, however, sub-beans do not have to share a common parent type.
Two <bean>s with completely different values in their class attributes can still
share a common set of properties that are inherited from a parent bean.
Consider the following abstract parent <bean> declaration:
<bean id="basePerformer" abstract="true">
<property name="song" value="Somewhere Over the Rainbow" />
</bean>
instrument=saxophone
song="Jingle Bells"
baseSaxophonist:Instrumentalist
kenny david
song="Mary had a..."
frank
The frank bean overrides
the song property
Figure 3.3 The frank bean inherits from the baseSaxophonist bean, but
overrides the song property.
78 CHAPTER 3
Advanced bean wiring
This basePerformer bean declares the common song property that our two performers
will share. But notice that it doesn’t have a class attribute set. That’s
okay, because each child bean will determine its own type with its own class
attribute. The new declarations of taylor and stevie are as follows:
<bean id="taylor"
class="com.springinaction.springidol.Vocalist"
parent="basePerformer" />
<bean id="stevie"
class="com.springinaction.springidol.Instrumentalist"
parent="basePerformer">
<property name="instrument" ref="guitar" />
</bean>
Notice that these beans use both the class attribute and the parent attribute
alongside each other.[/size]