This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Spring and the fly likes How to create new Bean instance every time Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Frameworks » Spring
Reply Bookmark "How to create new Bean instance every time" Watch "How to create new Bean instance every time" New topic
Author

How to create new Bean instance every time

Guy Roth
Ranch Hand

Joined: Aug 09, 2009
Posts: 74
I would like that every time that I'll perform :
SpringUtil.lookupComponent("myObject")
I spring will create new instance of this object

I set this bean in the relevant xml file as: scope="prototype"
but still spring created new instance on the first time and from then, every time that the same bean performed again
SpringUtil.lookupComponent("myObject") spring didn't generate new instance but returned the same old "MyObject" does anyone know why and how I can
force the lookup command to generate new instance for me.

I also saw in Spring documentation that the definition of scope prototype is:
Scopes a single bean definition to any number of object instances.

Which means that Spring doesn't necesseraly generate new instance every time that the same parent bean requires it.

Hong Anderson
Ranch Hand

Joined: Jul 05, 2005
Posts: 1936
Could you please to post the code of SpringUtil?


SCJA 1.0, SCJP 1.4, SCWCD 1.4, SCBCD 1.3, SCJP 5.0, SCEA 5, SCBCD 5; OCUP - Fundamental, Intermediate and Advanced; IBM Certified Solution Designer - OOAD, vUML 2; SpringSource Certified Spring Professional
Guy Roth
Ranch Hand

Joined: Aug 09, 2009
Posts: 74
Thanks for your help!

the problem was indeed in our internal code of SpringUtil (I though that SpringUtil is Springs original code and not our invention)

so That's it. It is clear now

Hong Anderson
Ranch Hand

Joined: Jul 05, 2005
Posts: 1936
Thanks for the update.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: How to create new Bean instance every time
 
Similar Threads
@Resource question
object which knows runtime parameters and spring bean
Are spring beans thread-safe?
basic doubt regarding beans
Singleton scope problem