是否可以在两个地方使用 Spring 自动装配原型作用域类的同一个实例
** 更改了示例以更好地表达
我正在使用 spring 2.5 的情况,并遇到以下情况
@Component
@Scope("prototype")
Class Foo
{
}
class A
{
@Autowired
Foo fooA;
}
class B
{
@Autowired
Foo fooB;
}
class C
{
@Autowired
Foo fooC;
}
我试图了解是否有某种方法可以使用 @Autowired
并绑定相同的 实例FOO 到
fooA
和 fooB
将不同的实例绑定到 fooC
时,
我明白如果 FOO
的范围将是 singleton
它会起作用,
但我正在徘徊是否有一种方法可以在使用 protoype
范围时实现相同的目标。
另请解释这是自动装配概念的正确用法吗?我是否试图滥用 Spring 框架的目的
** 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
ontofooA
and fooB
while binding a different instance to fooC
i understand that 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
由于
singleton
和prototype
范围似乎都不适合您(您不需要单个对象,但您也不希望每次都有一个新实例),因此您需要另一个范围。在 Web 应用程序上下文中,有一个现成的解决方案 - 使用
request
范围 - 因此,在每个请求/响应周期中,无论您在何处以及注入多少次,您都将只有一个 bean 实例。在非Web应用程序上下文中,您可以定义自己的
org.springframework.beans.factory.config.Scope
更新:在您澄清之后,这似乎是一个非常奇怪的情况。我想到的是:
FactoryBean
(实际上 -AbstractFactoryBean
的子类) - 一个每次返回新对象,一个返回相同的对象(两者其中应该在singleton
范围内)@Resource(name="prototypeFactoryBean")
和@Resource( name="singletonFactoryBean")
(而不是@Autowired
)singletonFactoryBean
可以设计为只返回一个单例(注入到工厂bean类中)BeanFactory
(可通过getBeanFactory()
获得)强制转换为AutowireCapableBeanFactory
并调用.autowire(newlyCreatedBean)
,然后返回它。 (或者你可以注入一个ApplicationContext
并获取它的AutowireCapableBeanFactory
)但这过于复杂,即使在我解释之后你也需要扩展 spring 知识:)
此外,我认为你应该重新考虑您的设计,而不是做出上述“怪癖”
更新 2: 在您发表评论后,命名概念将转移到注释中 - 正如我上面指出的,您可以使用
@Resource(name="someBean" )
Since neither
singleton
norprototype
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:
FactoryBean
s (actually - subclasses ofAbstractFactoryBean
)- one returning new object every time, and one returning the same object (both of them should be insingleton
scope)Foo
s with@Resource(name="prototypeFactoryBean")
and@Resource(name="singletonFactoryBean")
(instead of@Autowired
)singletonFactoryBean
can be designed to just return a singleton (injected in the factory bean class)prototypeFactoryBean
can create a new instance, cast theBeanFactory
(available throughgetBeanFactory()
) toAutowireCapableBeanFactory
and call.autowire(newlyCreatedBean)
, and then return it. (alternatively you can inject anApplicationContext
and get itsAutowireCapableBeanFactory
)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")
prototype
范围的全部要点是您每次都会获得不同的实例。另外,自动装配原型范围的 bean 在设计方面是有问题的(事实上,如果允许的话,我会感到有点惊讶)。通常的想法是将相同范围的 bean 自动装配在一起(有一些方法可以解决这个问题,但这里不相关)。
关于您的设计的一切都表明
Foo
不应该是原型 - 为什么要这样做?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.