@Qualifier(“unique_name”) 在 spring 中唯一标识应用程序上下文中的 bean 不起作用?
我有以下场景:
class A with "bean-a" in wireup.xml
class B extends A with "bean-b" in wireup.xml
由于 B 也是 A
,这意味着应用程序上下文中有多个类 A 的实例。
我想唯一标识类 B 的单例实例,因此我使用了
@Qualifier("unique_name") in class B
and
<qualifier value="unique_name"/> in "bean-b" of wireup.xml
但问题仍然存在,我收到的消息为:
No unique bean of type [A] is defined: expected single matching bean but found 2: [A, B]
如何解决此问题?
谢谢
I have the following scenario:
class A with "bean-a" in wireup.xml
class B extends A with "bean-b" in wireup.xml
Since B is also A
, which means there are more than one instance of class A in application context.
I want to uniquely identify the singleton instance of class B, so I used
@Qualifier("unique_name") in class B
and
<qualifier value="unique_name"/> in "bean-b" of wireup.xml
But the issue is still there, I get message as :
No unique bean of type [A] is defined: expected single matching bean but found 2: [A, B]
How do I resolve this?
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
@Qualifier 不应该放在 classB 中。相反,它应该与自动装配一起放置。
你的类应该看起来像这样,
这意味着你告诉 autowire b 类型为 B(类型为 B 或 A,因为 B 是 A 的子类),但 bean 定义具有限定符“unique_name”。 wireup.xml 具有分配给 bean-b 的限定符标记。你的wireup.xml可能看起来像这样
The @Qualifier should not be put in classB. Instead it should be put along with autowired.
Your class should look like,
By this means you are telling autowire the b with of type B (type B or A because B is subclass of A) but with a bean definition which has the qualifier "unique_name". The wireup.xml has the qualifier tag assigned to bean-b. Your wireup.xml might look like this