Singleton fundamentals.

16 views
Skip to first unread message

Gavin Baumanis

unread,
Dec 3, 2010, 12:36:10 AM12/3/10
to ColdSpring-Users
Hi Everyone,

I've been using CS now for nearly year and have decided that I don't
actually really understand what is going on.
So I thought better ask a few questions from those in the know and
shore up my understanding.
Because I am no longer confident that I am actually using it
appropriately.

(I have some singleton and transient questions - so I'll separate them
into separate threads so as to not get too long winded in one.)

Also I have just re-read through;
http://coldspringframework.org/coldspring/examples/quickstart/index.cfm?page=home

Although the answers to my questions can be inferred from the docs -
I'd rather have a definitive answer.


I have the following code in - Application.cfc /
onApplicationStart()
application.beanFactory = createObject("component",
"coldspring.beans.DefaultXmlBeanFactory").init();
application.beanFactory.loadBeansFromXmlFile("/com/mypath/to/objects/
coldspring_revisions.xml", true);

Now I want to use one of the objects that are marked as being a
singleton in that XML.

I know I *can" use the singleton and it's methods by doing;
<cfset myResult =
application.beanfactory.getbean('mybean').myMethod() />

so far so good.

* my question is upon the next method call of "mybean" (In a
completely separate request;
<cfset myOtherResult =
application.beanfactory.getbean('mybean').myOtherMethod() />
- is the object recreated or is it actually "within" the factory - in
this case in the application scope.

* If for "whatever" reason I created the beanfactory in the session
scope - does that mean that my singleton objects are effectively
session scoped too?

* would I "ever" need to do something like this?;
<cfset application.myCFCName =
application.beanfactory.getbean('mybean') >

and then use application.myCFCName - as if I had created the object
myself using CF's createObject function.
<cfset myLastResult = application.myCFCName.myMethod() />

* and If I did the above - have I actually just gone and created a new
instance of the class?

As always - thanks!

(Ps. I'd be happy to tackle updating the guide if people think these
questions/answers are a sensible addition)


Gavin.

Barney Boisvert

unread,
Dec 3, 2010, 12:47:28 AM12/3/10
to coldspri...@googlegroups.com
See answers inline below:

On Thu, Dec 2, 2010 at 9:36 PM, Gavin Baumanis <be...@palcare.com.au> wrote:
>  * my question is upon the next method call of "mybean"  (In a
> completely separate request;
> <cfset myOtherResult =
> application.beanfactory.getbean('mybean').myOtherMethod() />
>  - is the object recreated or is it actually "within" the factory - in
> this case in the application scope.

ALL invocations of getBean("myBean") will return the same CFC instance
for the life of the beanFactory. That's what it means to be a
singleton.

>  * If for "whatever" reason I created the beanfactory in the session
> scope - does that mean that my singleton objects are effectively
> session scoped too?

Yep, your singletons will be session scoped for the same reason as
above: singletons have exactly one instance for the life of the
beanFactory.

>  * would I "ever" need to do something like this?;
> <cfset application.myCFCName =
> application.beanfactory.getbean('mybean') >
>
> and then use application.myCFCName - as if I had created the object
> myself using CF's createObject function.
> <cfset myLastResult = application.myCFCName.myMethod() />

No, there's never a compelling reason to do that. Using an external
variable can reduce verbosity and it avoids the repeated invocations
of getBean, but there isn't any "real" benefit of an external
variable. I frequently do <cfset myObj =
application.beanFactory.getBean("myNeatSingletonObject") /> at the top
of my controllers so I can just do myObj.neatMethod(), which is a lot
easier to read.

> * and If I did the above - have I actually just gone and created a new
> instance of the class?

Nope, you've just created an additional reference to the SAME instance
of your CFC.

cheers,
barneyb

--
Barney Boisvert
bboi...@gmail.com
http://www.barneyb.com/

Gavin Baumanis

unread,
Dec 3, 2010, 1:05:07 AM12/3/10
to ColdSpring-Users
Hi Barney,

It would seem that I had the right idea all along then - but it's
always good to have a sanity check!
Thanks very much - I appreciate the hellp.

Gavin.



On Dec 3, 4:47 pm, Barney Boisvert <bboisv...@gmail.com> wrote:
> See answers inline below:
>
> bboisv...@gmail.comhttp://www.barneyb.com/

Sean Corfield

unread,
Dec 3, 2010, 1:59:05 AM12/3/10
to coldspri...@googlegroups.com
On Thu, Dec 2, 2010 at 10:36 PM, Gavin Baumanis <be...@palcare.com.au> wrote:
>  * would I "ever" need to do something like this?;
> <cfset application.myCFCName =
> application.beanfactory.getbean('mybean') >

The only reason I can suggest is performance: ColdSpring 1.x has some
overhead in getBean() and you _can_ (sometimes) get a performance
improvement by keeping direct references to the CFC instances that
ColdSpring creates. But I really wouldn't recommend it. As Barney
says, application.beanFactory.getBean('mybean') will always return the
same instance and that should be all you'll need.
--
Sean A Corfield -- (904) 302-SEAN
Railo Technologies, Inc. -- http://getrailo.com/
An Architect's View -- http://corfield.org/

"If you're not annoying somebody, you're not really alive."
-- Margaret Atwood

Reply all
Reply to author
Forward
0 new messages