2个名称相同但包装不同的豆;如何自动接线? [英] 2 beans with same name but in different packages; how to autowire them?

查看:89
本文介绍了2个名称相同但包装不同的豆;如何自动接线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,其中有两个具有相同名称但位于不同程序包中的bean.我的Spring应用程序失败,因为它无法决定采用哪个bean.有什么解决办法吗? Bean当前未实现特定的接口.

I have an application that has 2 beans with the same name, but which are in different packages. My Spring application fails because it cannot decide on which bean to take. Is there any solution for this? The beans do not currently implement specific interfaces.

请参见下面的例外编辑示例:

See below an edited example of the exception:

Caused by:
org.springframework.context.annotation.ConflictingBeanDefinitionException:
Annotation-specified bean name 'dataTransferHandler' for bean class
[aaaaa.ws.handler.DataTransferHandler] conflicts with existing,
non-compatible bean definition of same name and class
[bbbbb.ws.handler.DataTransferHandler]

推荐答案

您将不得不为您的bean提供不同的名称-如果使用相同的名称定义了多个bean,则稍后定义的bean将覆盖之前定义的bean-因此在您的情况下,将仅存在一个名称为dataTransferHandler的bean.

You will have to give your beans different names - if multiple beans are defined with the same name, then the one defined later will override the one defined earlier - so in your case only one bean will exist with the name of dataTransferHandler.

您可以给这两个bean提供不同的名称,以便两者都可以存在,并且可以使用以下方法注入正确的一个: @AutoWired @Qualifier("dataTransferHandler") 或者 @Resource(name="dataTransferHandler")

You can give these two beans different names, so that both can exist and you can inject in the correct one either using: @AutoWired @Qualifier("dataTransferHandler") OR @Resource(name="dataTransferHandler")

这篇关于2个名称相同但包装不同的豆;如何自动接线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆