Defining and Using Parent Beans

One issue that some people eventually run into when using ColdSpring is duplication in their XML configuration file. Consider something like this:

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
	
	<bean id="userGateway" class="coldspring.examples.quickstart.components.UserGateway">
		<property name="configBean">
			<ref bean="configBean" />
		</property>
	</bean>
	
	<bean id="invoiceGateway" class="coldspring.examples.quickstart.components.InvoiceGateway">
		<property name="configBean">
			<ref bean="configBean" />
		</property>
	</bean>
	
	<bean id="productGateway" class="coldspring.examples.quickstart.components.ProductGateway">
		<property name="configBean">
			<ref bean="configBean" />
		</property>
	</bean>
	
	<bean id="configBean" class="coldspring.examples.quickstart.components.ConfigBean" />
	
</beans>

In this case, we have three Gateway components that all depend on the ConfigBean. But we're duplicating the dependency in all three Gateway definitions in the configuration file. That's not good, and if we had 20 Gateway's it would be even worse.

ColdSpring allows us to eliminate the duplication by defining a parent bean. What this lets us do is move the dependency to one central bean, and then tell ColdSping that our Gateways all require the dependencies defined in that common parent bean:

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
	
	<bean id="parentGateway" abstract="true">
		<property name="configBean">
			<ref bean="configBean" />
		</property>
	</bean>
	
	<bean id="userGateway" parent="parentGateway" class="coldspring.examples.quickstart.components.UserGateway" />
	<bean id="invoiceGateway" parent="parentGateway" class="coldspring.examples.quickstart.components.InvoiceGateway" />
	<bean id="productGateway" parent="parentGateway" class="coldspring.examples.quickstart.components.ProductGateway" />
	
	<bean id="configBean" class="coldspring.examples.quickstart.components.ConfigBean" />
	
</beans>

You can see we got rid of all the duplicate XML. Now, all of our Gateways will still have the ConfigBean injected into them, but we only needed to define that dependency in the parent bean. This can be very handy with similar CFCs that have common dependencies.

Parent Beans vs. CFC Inheritance

People commonly get confused by the parent bean functionality because on the surface it resembles CFC inheritance. While the two are similar in concept, they actually have nothing to do with each other.

In CFCs, one component can extend another component. This is called inheritance, and is a common part of object-oriented development. We can define data and methods in the superclass (the object being extended) which will be available in the subclasses (the children).

ColdSpring parent beans are not tied to CFC inheritance. In fact, it is very possible to define a parent bean in ColdSpring that has no corresponding CFC at all. It exists for no other purpose than to reduce duplication in the XML configuration file.

Now under the hood, our Gateway CFCs might actually extend a "BaseGateway", and the setConfigBean() method may be defined in the BaseGateway and inherited by all of the concrete Gateways. But this is not required for the parent bean functionality to work.

To summarize, whether our Gateway CFCs inherit the setConfigBean() method from a base CFC, or whether each one defines its own setConfigBean() method, has no connection to the ColdSpring parent bean we defined. All the parent bean does is eliminate the duplicate XML in our Gateway bean definitions.