So it's not an injection mechanism really then -- just a nicer place to put some advanced configuration code? Darn :) I can live with setter injections, just thought this might've been a cleaner way to go about it.
Jonathon -----Original Message----- From: Peter Bell [mailto:[EMAIL PROTECTED] Sent: Monday, July 09, 2007 4:26 PM To: [email protected]; Jonathon Stierman Subject: Re: [coldspring-dev] init-method usage Hi Jonathon, Init-method isn't really for resolving circular dependencies. If you have those, use setter rather than construction injection and all will be good. The goal of the init-method is to allow you to call an additional method after the constructor AND setter injection - typically for post setter injection configuration. You will often use init-method where you have code that needs to be run before your bean is returned by CS but which depends on another bean that is setter injected. Lets say UserService and AddressService depend on each other, so both use setter injection. If UserService needs to call AddressService.something() to fully initialize itself. Throw that code into another method (you might call it config() - that seems to be a common convention), and then the init-method for UserService is config. That is the use case for init-method. Best Wishes, Peter On 7/9/07 3:06 PM, "Jonathon Stierman" <[EMAIL PROTECTED]> wrote: > Anyone have any examples of how this is used? I¹ve got a circular > dependency that I¹d like to resolve by using this. > > I got a response from Peter Farrell, in a past thread: > >>> <bean name="someService" id="dot.path.to.someService" > init-method="setup"> >>> <property name="someOtherService"><ref bean="someOtherService"/></bean> >>> <property name="thatService"><ref bean=""thatService"/></bean> >>> </bean> >>> >>> Order of operations: >>> 1. Create someService >>> 2. Call init() with any constructor args (must resolve dependencies > first) >>> 3. Wiring in properties >>> 4. Call init-method (yes, it's a misleading name, but it follows the > Spring DTD here). In my architecture, we >>> call this the setup() method. > > 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 > > >
