Is It Possible To Spring Autowire the same Instance of a protoype scoped class in two places?

Since neither singleton nor prototype scopes seem to fit you (you don't want a single object, but you don't want a new instance each time), you need another scope.

Since neither singleton nor prototype scopes seem to fit you (you don't want a single object, but you don't want a new instance each time), you need another scope. In a web-application context there is a ready solution - use request scope - thus in every request/response cycle you will have only one instance of your bean, no matter where and how many times you inject it. In a non-web application context you can define your own implementation of org.springframework.beans.factory.config.

Scope Update: after you clarified, this seems like a very strange case. What comes to my mind is the following: define two FactoryBeans (actually - subclasses of AbstractFactoryBean)- one returning new object every time, and one returning the same object (both of them should be in singleton scope) inject the Foos with @Resource(name="prototypeFactoryBean") and @Resource(name="singletonFactoryBean") (instead of @Autowired) the singletonFactoryBean can be designed to just return a singleton (injected in the factory bean class) the prototypeFactoryBean can create a new instance, cast the BeanFactory (available through getBeanFactory()) to AutowireCapableBeanFactory and call . Autowire(newlyCreatedBean), and then return it.(alternatively you can inject an ApplicationContext and get its AutowireCapableBeanFactory) But this is overly complex and you will need extended spring knowledge even after my explanation :) Furthermore I think you should reconsider your design instead of making the above 'quirks' Update 2: After your comment, the naming concept is transferred to annotations - as I indicated above you can use @Resource(name="someBean").

I am using a non-web configuration, so the suggested solution does not apply, also my explanation above was oversimplified I will revise it – Mark Feb 4 '10 at 9:59 thanks . I agree with all you said above. I know it seems like a weird scenario but if you think about it there are many occasions in a complex project where several instances of a specific class are shared among other classes.

If you think about the spring XML configuration then it is handles very simply by "naming" , you just define two beans with different names represented by the same "code" I was merely trying to see if this capability was transferred to annotations, it seems a little inconsistent – Mark Feb 4 '10 at 15:57.

The whole point of prototype scope is that you get a different instance each time. Also, autowiring a prototype-scoped bean is questionable, design-wise (in fact, I'd be mildly surprised if it was even permitted). The usual idea is to autowire together beans of the same scope (there are ways around this, but not relevant here).

Everything about your design suggests that Foo should not be a prototype - why have you made it so?

The example above is a simplified expression of my situation I am using a bean in the form of prototype because it is needed in my current configuration, there are many places where the bean is injected and a new instance is needed for each of them. But there is a specific place where I would like two objects to share the same instance of the a third object and I wanted to see if it is supproted by spring – Mark Feb 4 '10 at 9:54 Perhaps you could configure multiple Foo singleton beans, and wire them up individually, using @Autowired @Qualifier – skaffman Feb 4 '10 at 10:03.

Autowiring prototype scoped objects is perfectly possible, but a new instance will be created each time. So to answer your question: no, you can't do that. Your usage of component scanning and autowiring seems ok for the other part.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions