Jonathon Stierman said the following on 7/9/2007 2:06 PM:

I figured ColdSpring would just run some magic on my properties and
automatically figure out that they should be set in the init-method rather
than in the init() constructor, but CS threw an error at me indicating it
was still looking for a generic set[myDependency]() method.  Here’s my
config.xml:

<!-- File loaded by ColdSpring to perform dependency injection -->
<beans>
        <bean id="Articles" class="Components.Articles" singleton="true">
                <constructor-arg
name="datasource"><value>${datasource}</value></constructor-arg>
        </bean>
        <bean id="Categories" class="Components.Categories" singleton="true"
init-method="setup">
                <constructor-arg
name="datasource"><value>${datasource}</value></constructor-arg>
                <property name="articleCFC"><ref bean="Articles"
/></property>
        </bean>
</beans>

And here’s my Categories.cfc:

<cfcomponent hint="Add/Edit/Delete Categories">
        <cfset Variables.datasource          = "" />
        <cfset Variables.articlesCFC = "" />
        
        <cffunction name="init" access="public"
returntype="Components.Categories" hint="I initialize myself">
                <cfargument name="datasource"              type="string"
required="false" />
                
                <cfset Variables.datasource = arguments.datasource />
                
                <cfreturn this />
        </cffunction>
        
        <cffunction name="setup" access="public"
returntype="Components.Categories" hint="I post-initilize myself for
circular dependencies">
                <cfargument name="articlesCFC"
type="Components.Articles"    required="false" />
                
                <cfset Variables.articlesCFC = arguments.articlesCFC />
                
                <cfreturn this />
        </cffunction>
</cfcomponent>

What’s the syntax for making the init-method concept work?  I googled around
a bit, but kept getting matches regarding the regular init() constructor
rather than the init-method usage.

Jonathon
Doesn't appear that my original message hit the list.  Here it is again...

<beans>
        <bean id="Articles" class="Components.Articles" singleton="true">
                
<constructor-argname="datasource"><value>${datasource}</value></constructor-arg>
        </bean>
        <bean id="Categories" class="Components.Categories" singleton="true" 
init-method="setup">
                <constructor-arg 
name="datasource"><value>${datasource}</value></constructor-arg>
                <property name="articleCFC"><ref bean="Articles"/></property>
        </bean>
</beans>

The property tag requires a setter for dependencies injection. Like this:

<cfcomponent hint="Add/Edit/Delete Categories">
        <cfset Variables.datasource = "" />
        <cfset Variables.articlesCFC = "" />
        <cfset variables.somethingThatNeedsToBeDonePostDependencyResolution = "" 
/>
        
        <cffunction name="init" access="public" returntype="Components.Categories" 
hint="I initialize myself">
                <cfargument name="datasource" type="string" required="false" />
                <cfset variables.datasource = arguments.datasource />
                <cfreturn this />
        </cffunction>
        
        <cffunction name="setArticlesCFC" access="public" returntype="void" 
hint="Dependency: injected">
                <cfargument name="articlesCFC" type="Components.Articles" 
required="true" />
                <cfset variables.articlesCFC = arguments.articlesCFC />
        </cffunction>

        <cffunction name="setup" access="public" returntype="void" hint="Post 
initialization stuff">
                <cfset 
variables.somethingThatNeedsToBeDonePostDependencyResolution = 
variables.articlesCFC.something() /
        </cffunction>

</cfcomponent>


The setup() method (using init-method) is used if the CFC that you're wiring needs to perform some type of processing on stuff *after* all the dependencies have been resolved. ColdSpring does *not* inject constructor arg style elemented into your setup method -- I added it to my example above.

.Peter

Reply via email to