Injecting the Coldspring bean factory itself

16 views
Skip to first unread message

Jesus V.

unread,
Sep 24, 2009, 8:30:44 AM9/24/09
to ColdSpring-Users
Dear all,

kind of stupid question but, is there any way for injecting the
Coldspring bean factory itself to other beans?

I want some bean to be able to create other type of bean. So I thought
of injecting the OtherBeanFactory, but why not using the already
existing Coldspring factory itself?

Is this anyhow possible?

Thanks for your help,
Jesús

Kevin Pepperman

unread,
Sep 24, 2009, 8:37:03 AM9/24/09
to coldspri...@googlegroups.com
If I recall correct all you have to do is add this to the bean.

    <!--- Dependency injection methods for Bean Factory. --->
    <cffunction name="getBeanFactory" access="public" returntype="any" output="false" hint="I return the BeanFactory.">
        <cfreturn variables.instance.beanFactory />
    </cffunction>
       
    <cffunction name="setBeanFactory" access="public" returntype="void" output="false" hint="I set the BeanFactory.">
        <cfargument name="beanFactory" type="coldspring.beans.BeanFactory" required="true" />
        <cfset variables.instance.beanFactory = arguments.beanFactory />
    </cffunction>
--
-- if you've written a homegrown[*CFML] controller layer that rivals the sophistication and ease of use of the three major players (MG/MII/FB) - then share it or shut up. You code in a silo - that's great. Just stay in there and don't try to tell everyone how good it smells.
--Dave Ross

Jesus V.

unread,
Sep 24, 2009, 10:47:30 AM9/24/09
to ColdSpring-Users
Kevin,

doing just that does not work ('beanFactory' does not exists in
'variables.instance'). Don't I have to set up also some bean's
property in the coldspring.xml file?

Jesús

Brian Kotek

unread,
Sep 24, 2009, 11:20:14 AM9/24/09
to coldspri...@googlegroups.com
No, that's how you do it. Can you post the rest of your CFC?

Jesus V.

unread,
Sep 25, 2009, 3:16:11 AM9/25/09
to ColdSpring-Users
This is a simplified example, but it explains the important point:

ObjectDao.cfc
---------------------
<cfcomponent ...>

...

<cffunction name="load" returntype="Object" ...>
<cfargument name="id" ...>

<cfquery name="qObjects" ...>
...
</cfquery>

<cfif qQuery.recordcount eq 1>
<cfset newObject = getObjectFactory().getBean("Object")>
<cfset newObject.setId(qObject.id)>
<cfset newObject.setProperty(qObject.property_field)>
...
<cfreturn newObject>
</cfif>

...

<!--- Dependency Injection --->
<cffunction name="getObjectFactory" returntype="any" ...>
<cfreturn variables._instance.objectFactory>
</cffunction>
<cffunction name="setObjectFactory" returntype="void" ...>
<cfargument name="objectFactory" ...>

<cfset variables._instance.objectFactory =
arguments.objectFactory>
</cffunction>

</cfcomponent>


coldspring.xml
---------------------
...
<bean id="Object" class="path.to.my.Object" />
<bean id="ObjectFactory" class="path.to.my.ObjectFactory" />
<bean id="ObjectDao" class="path.to.my.ObjectDao">
<constructor-arg name="dsn"><value>myDsn</value></constructor-arg>
<property name="objectFactory"><ref bean="ObjectFactory" /></
property>
</bean>
...

Enough typing!

Ok, this works but I had to create an ObjectFactory class and I was
wondering if it is possible to inject the already existing Coldspring
bean factory instead, i.e. writing something like:

<bean id="ObjectDao" class="path.to.my.ObjectDao">
<constructor-arg name="dsn"><value>myDsn</value></constructor-arg>
<property name="objectFactory"><ref bean="ColdspringBeanFactory" /
></property>
</bean>

Any ideas?

Jesús

Kevin Pepperman

unread,
Sep 25, 2009, 4:22:10 AM9/25/09
to coldspri...@googlegroups.com
Well, The method I gave you works, but I don't think you can inject it as a constructor-arg like you are describing.

It is autowired by the factory when the service is first initialized, you just need to add that code I posted exactly the way its posted, because  is HAS to be called setBeanFactory and the type on the setter needs to be type="coldspring.beans.BeanFactory".


Also, It looks like you are incorrectly trying to use the ColdSpring Factory to serve up Transient Beans (business objects), which it will do correct only if you use  singleton="false" in the bean tag.

 But this is not really what ColdSpring it was designed for, and the performance penalty for using it to get transients makes it best practice to either create your own Transient Factory or just make the bean in your service with createObject().

http://www.coldspringframework.org/coldspring/examples/quickstart/index.cfm?page=singletons


ColdSpring is for managing Singletons, If you use this method you posted, ColdSpring will always return the same exact(singleton) object from memory.

Even if you change variables in that object, it will always still be the same object everywhere its called, if someone else comes along and calls the same service right after you called it, whatever the variables are set to will be in that singleton wherever it has been called.


For an example how you can use the coldSpring factory in your own services, Look at ColdSpring Bean Utilities >  BeanInjector.cfc

It uses the ColdspringFactory to inject Singletons into transient beans.

http://coldspringutils.riaforge.org/


Also look at metro, It has a TransientFactory.cfc which uses the coldspringutils bean injector and the ColdSpring factory to create your transient beans for you, as well as providing a way to inject any ColdSpring managed singleton into the transient bean when it is created.

http://metro.riaforge.org/

Hope that helps.

/Kevin Pepperman


Jesus V.

unread,
Sep 25, 2009, 9:53:46 AM9/25/09
to ColdSpring-Users
Sure it helps, thanks a lot Kevin.

Jesús

On Sep 25, 10:22 am, Kevin Pepperman <chorno...@gmail.com> wrote:
> Well, The method I gave you works, but I don't think you can inject it as a
> constructor-arg like you are describing.
>
> It is autowired by the factory when the service is first initialized, you
> just need to add that code I posted exactly the way its posted, because  is
> HAS to be called setBeanFactory and the type on the setter needs to be
> type="coldspring.beans.BeanFactory".
>
> Also, It looks like you are incorrectly trying to use the ColdSpring Factory
> to serve up Transient Beans (business objects), which it will do correct
> only if you use  singleton="false" in the bean tag.
>
>  But this is not really what ColdSpring it was designed for, and the
> performance penalty for using it to get transients makes it best practice to
> either create your own Transient Factory or just make the bean in your
> service with createObject().
>
> http://www.coldspringframework.org/coldspring/examples/quickstart/ind...

Kevin Pepperman

unread,
Sep 25, 2009, 7:34:33 PM9/25/09
to coldspri...@googlegroups.com
This was very hard for me to digest at first, don't give up!

Spend a day with just the examples provided, get the simplest things working first, then move on. Stay away from your website project at first, it will only cause problems learning it.

If you try to just plug this into something you are doing, you will just get frustrated because errors bounce back from places that have nothing to do with what actually caused them.


So get a working test suite first, make sure that everything does what you intended all by itself, and then you grasp what it it doing by changing little stuff, then move on.

These frameworks and bean utility methods are made by some brilliant people that are literal saints for making the code and knowledge available for us mere mortals, (yes, you Corfield, Kotek, Marcotte etc.. I know you all read all this stuff)

Learning this will help you build your site on the "back of giants" and will make your code much more powerful and usable. 


Once I grasped ColdSpring, (after several failed attempts and much frustration) I never looked back.

When I finally understood the bean injector methods; and how to use rich business objects with ColdSpring injected singletons, things really took off for me.

I can test things in ways I never though possible, I can even test on a fully live service by injecting it into a test bean or vic-versa, these very powerful tools that will change the way you develop.


I can never give enough credit to all the people who contribute to these projects.

Without their fine work my learning would be stagnate and I would still be wading through my old procedural code trying to add a field to my database.


Much respect to all those involved.. you know who you are!


/Kevin Pepperman


Reply all
Reply to author
Forward
0 new messages