Hello guys, As Spring document mentioned . lookup method is used to inject a non-singleton bean into a singleton bean. I want to know can I use it to inject a non-singleton bean into a non-singleton. I mean can I declare an abstract look-up method in a non-singleton bean , called abstract MyBean createMyBean(); I link this lookup method to another non-singleton bean(Mybean), each time when I call this look-method it returns me new instance of "MyBean" . I want to use this way to instead of "ObjectFactoryCreatingFactoryBean"
My recommendation is to just try it out in some simple project. Shouldn't be that difficult to create.
My question would be, why does the bean that gets injected into need to be a Singleton? When looking at the documentation and example, it seemed this was the case where you had some type of "Manager/Controller" type class that needs another class injected into it that is dynamic. But in those kind of designs, you pretty much always want that "Manager/Controller" class to be a singleton in your Application. Not that there might be some design that could need both to be prototype, I am just curious to that design?
And I think non-singleton to non-singleton can just use the standard dependency declarations, as I see here state in this link.
Hello Mark, Thank you for your reply. The reason I want to use lookup method to inject a bean to a non-singleton bean is that I need to create several jms listeners(non-singleton bean) in my application,each listener need to create new bean when it receive jms data from the Queue. jms listener beans are running in spring jms container. If I use the normal injection, I couldn't get new bean each time when listener receive the data from Queue. So, when I declare a look-up method in my listener bean, I can call this method to get a new bean each time.(look-up method can return me different instance each time , I have tested it.) Of couse I also can use "ObjectFactoryCreatingFactoryBean" to inject a object factory into my listener bean. But ,I want to know which one is better or which one is the correct way. Since both of them can do the same job.
In the link I posted, it said you could have two scope prototypes, and not need lookup-method. That you only needed the lookup-method when one was Singleton and one was Prototype.
So I recommend trying that out and seeing what happens first.
OK, so JMS and you have a Message Driven Pojo, I still don't think you need to make the MDP a prototype. that Singleton is absolutely fine for JMS. If you thing one class to handle all the incoming messages is a bottleneck, then I disagree with that, unless you are using Synchronization on your code, which isn't a good idea.
Joined: Sep 01, 2004
Hello Mark, I did a test by using look-up method and objectFactoryBean to create bean2 in bean1(bean1 is singleton). Both of them are working fine. So my question is that why don't use ObjectFactoryCreatingFactoryBean to create new bean in a singleton bean. Can I use ObjectFactoryCreatingFactoryBean as a replacement for look-up method. The link you give me which mentioned ServiceLocatorFactoryBean, I guess it's the same thing with ObjectFactoryCreatingFactoryBean.
You are right,Message Driven pojo is a singlton bean. I'm using Message Driven pojo to handle the jms request, but one pojo is not enough in my project, I need to run some business process inside the jms listener(Pojo). And It takes some time to finish the business process. So, I create multiple pojo in jms container.
Each Pojo need to create a new bean when the new jms data coming. But I can't decide which way should I use to create the new bean.(look-up method or objectFactoryBean) I guess maybe objectFactoryBean is better, because I don't need to use CGLIB. Thank you for your reply.
[ November 04, 2008: Message edited by: mark ken ]