'Parent' attribute on Beans - Please define

1 view
Skip to first unread message

penny

unread,
Jul 30, 2008, 11:33:14 AM7/30/08
to ColdSpring-Users
Sorry if this has knowledge has already been conveyed, but I'm trying
to get the definition of what exactly the 'parent' attribute on the
Bean definition does - as I'm getting into a 'configSettings' bean
which contains dsn settings etc, and am currently having it be
injected into most all beans that need it - and trying to determine if
'parent=configsettings' is a better approach.

Thanks (awaiting the documentation update on this attribute)

Kevin Penny
Jobs2Web, Inc.

Brian Kotek

unread,
Jul 30, 2008, 11:52:36 AM7/30/08
to coldspri...@googlegroups.com
The parent attributes lets you specify a parent bean definition to augment dependency or configuration settings. So say you have a UserGateway and a ProductGateway, and you want to inject a Config bean into both of them. Instead of specifying a Config bean as a dependency to each one, you can create a third bean definition (maybe named "ParentGateway" or "AbstractGateway") with class="" and abstract="true", and specify the Config bean there. Then your gateways can specify parent="AbstractGateway" and both your gateways will have the Config bean wired into them.

In other words, it is simply a way to reduce XML by specifying common properties in one place rather than having to duplicate them in all of the necessary bean definitions. Make sense?

penny

unread,
Jul 30, 2008, 12:07:33 PM7/30/08
to ColdSpring-Users
Sure does - i'm going to paste an example here then and play with a
small implementation of it for a firm understanding.

Message has been deleted

penny

unread,
Jul 30, 2008, 5:12:03 PM7/30/08
to ColdSpring-Users
Ok I think I have it:

coldspring.xml definitions...
<!-- Regular Bean in need of some config settings (and specifies the
configGateway as the parent) -->
<bean id="regularBean" class="com.regularBean" parent="configGateway">

</bean>
<!-- Configuration Bean which holds config settings -->
<bean id="configSettings" class="com.configSettings">
<constructor-arg name="dsn">
<value>odbcdsn</value>
</constructor-arg>
</bean>
<!-- mock configGateway which has reference to the ConfigSettings -->
<bean id="configGateway" class="" abstract="true">
<property name="configSettings">
<ref bean="configSettings" />
</property>
</bean>

Then in my 'RegularBean' I need the methods for configSettings i.e.:
<!--- Author: penny - Date: 7/30/2008 --->
<!--- getter and setter for configSettings --->
<cffunction name="getconfigSettings" access="public" output="false"
returntype="com.configSettings">
<cfreturn variables.instance.configSettings/>
</cffunction>
<cffunction name="setconfigSettings" access="public" output="false"
returntype="void">
<cfargument name="configSettings" type="com.configSettings"
required="true"/>
<cfset variables.instance.configSettings=arguments.configSettings/>
</cffunction>

When calling Regular Bean -
application.serviceFactory.getBean('regularBean').getConfigSettings().getDSN()
for example - and voila I have the data -

This the best approach? So I will still require the 'stubbed' getter/
setter for configSettings in the Objects - but it slims down the xml
declaration a bit.

Reply all
Reply to author
Forward
0 new messages