spring 2个同名但在不同包中的bean;如何自动装配它们?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11015205/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 05:10:07  来源:igfitidea点击:

2 beans with same name but in different packages; how to autowire them?

springinversion-of-controlautowired

提问by Marco

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.

我有一个应用程序,它有 2 个同名的 bean,但它们在不同的包中。我的 Spring 应用程序失败,因为它无法决定采用哪个 bean。有什么解决办法吗?bean 目前没有实现特定的接口。

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]

回答by Biju Kunjummen

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 指定不同的名称——如果多个 bean 定义为相同的名称,那么稍后定义的 bean 将覆盖之前定义的 bean——因此在您的情况下,只有一个 bean 名称为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")

您可以为这两个 bean 赋予不同的名称,以便它们都可以存在,并且您可以使用以下任一方法注入正确的名称: @AutoWired @Qualifier("dataTransferHandler")OR @Resource(name="dataTransferHandler")

回答by Japan Trivedi

You can give attribute primary="true"to the bean defination you want to have the preference when autowired. But the bean names must be different. There is no solution for same bean name.

您可以将属性primary="true"赋予您希望在自动装配时具有首选项的 bean 定义。但是 bean 名称必须不同。相同的 bean 名称没有解决方案。

At run-time when you will get the autowired class then the primary true bean will get the preference for autowiring. Hope this helps you. Cheers.

在运行时,当您获得自动装配类时,主要的真正 bean 将获得自动装配的首选项。希望这对你有帮助。干杯。

回答by Dean Gurvitz

I asked another question regarding the same problem, and there is a solution that doesn't require using the @Qualifier annotation: if both of your DataTransferHandler classes have a @Component annotation, you can simply add a String argument to one of their constructions (i.e. @Component("Foo")), and that should solve the problem without needing additional changes.

我问了另一个关于同一问题的问题,有一个不需要使用 @Qualifier 注释的解决方案:如果你的两个 DataTransferHandler 类都有一个 @Component 注释,你可以简单地向它们的一个结构中添加一个 String 参数(即@Component("Foo")),这应该可以解决问题而无需额外更改。

See User9123's answer on my questionfor more details.

有关更多详细信息,请参阅User9123 对我的问题的回答