Inheriting Bean Configuration

Spring allows you to extract the common bean configurations to form a parent bean. The beans that inherit from this parent bean are called child beans. The child beans will inherit the bean configurations, including bean properties and attributes in the <bean> element, from the parent bean to avoid duplicate configurations. The child beans can also override the inherited configurations when necessary. The parent bean can act as a configuration template and also as a bean instance at the same time. However, if you want the parent bean to act only as a template that cannot be retrieved, you must set the abstract attribute to true, asking Spring not to instantiate this bean.

You must note that not all attributes defined in the parent <bean> element will be inherited. For example, the autowire and dependency-check attributes will not be inherited from the parent. To find out more about which attributes will be inherited from the parent and which won’t, please refer to the Spring documentation about bean inheritance.

<beans …>
<bean id="sequenceGenerator"
class="com.sequence.SequenceGenerator">
<property name="initial" value="100000" />
<property name="suffix" value="A" />
<property name="prefixGenerator" ref="datePrefixGenerator" />
</bean>
<bean id="sequenceGenerator1"
class="com.sequence.SequenceGenerator">
<property name="initial" value="100000" />
<property name="suffix" value="A" />
<property name="prefixGenerator" ref="datePrefixGenerator" />
</bean>
<bean id="datePrefixGenerator"
class="com.sequence.DatePrefixGenerator">
<property name="pattern" value="yyyyMMdd" />
</bean>
</beans>

To avoid duplicating the same properties, you can declare a base sequence generator bean with those properties set. Then the two sequence generators can inherit this base generator so that they also have those properties set automatically. You needn’t specify the class attributes of the child beans if they are the same as the parent’s.

<beans …>
<bean id="baseSequenceGenerator"
class="com.sequence.SequenceGenerator">
<property name="initial" value="100000" />
<property name="suffix" value="A" />
<property name="prefixGenerator" ref="datePrefixGenerator" />
</bean>
<bean id="sequenceGenerator" parent="baseSequenceGenerator" />
<bean id="sequenceGenerator1" parent="baseSequenceGenerator" />
</beans>

The inherited properties can be overridden by the child beans. For example, you can add a child sequence generator with a different initial value.

<beans …>
<bean id="baseSequenceGenerator"
class="com.sequence.SequenceGenerator">
<property name="initial" value="100000" />
<property name="suffix" value="A" />
<property name="prefixGenerator" ref="datePrefixGenerator" />
</bean>
<bean id="sequenceGenerator2" parent="baseSequenceGenerator">
<property name="initial" value="200000" />
</bean>
</beans>

The base sequence generator bean can now be retrieved as a bean instance to use. If you want it to act as a template only, you have to set the abstract attribute to true. Then Spring will not instantiate this bean.

<bean id="baseSequenceGenerator" abstract="true"
class="com.sequence.SequenceGenerator">
</bean>

Now SequenceGenerator and reverseGeneratordon’t extend the same base class—that is, they’re not in the same class hierarchy, but they have a property of the same name: initial. To extract this common initial property, you need a baseGeneratorparent bean with no class attribute defined.

<beans …>
<bean id="baseGenerator" abstract="true">
<property name="initial" value="100000" />
</bean>
<bean id="baseSequenceGenerator" abstract="true" parent="baseGenerator"
class="com.sequence.SequenceGenerator">
<property name="suffix" value="A" />
<property name="prefixGenerator" ref="datePrefixGenerator" />
</bean>
<bean id="reverseGenerator" parent="baseGenerator"
class="com.sequence.ReverseGenerator" />
<bean id="sequenceGenerator" parent="baseSequenceGenerator" />
<bean id="sequenceGenerator1" parent="baseSequenceGenerator" />
<bean id="sequenceGenerator2" parent="baseSequenceGenerator">
</bean>
</beans>

The resulting bean example of inherentance: