开发者

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

开发者 https://www.devze.com 2022-12-19 14:26 出处:网络
** changed the example to better express the situation i am using spring 2.5 and have the following situation

** changed the example to better express the situation

i am using spring 2.5 and have the following situation

@Component
@Scope("prototype") 
Class Foo
{
}

class A
{ 
  @Autowired 
  Foo fooA;
}


class B
{ 
  @Autowired 
  Foo fooB;
}



class C
{ 
  @Autowired 
  Foo fooC;
}

i am trying to understand if there is some way to use @Autowired and bind the same instance of FOO onto fooA and fooB while binding a different instance to fooC

i understand th开发者_运维技巧at if the scope of FOO will be singleton it will work

but i am wandering if there is a way to achieve the same goal while using a protoype scope.

also please explain is this the correct usage of the autowiring concept ? am i trying to abuse the spring framework purpose


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")


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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消