2个名称相同但包装不同的豆子;如何自动装配?

时间:2020-12-26 17:43:06

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.

我有一个应用程序,它有两个名称相同的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]

2 个解决方案

#1


13  

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以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”)或@ resource (name=“dataTransferHandler”)插入正确的名称

#2


0  

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.

您可以为您希望在自动连接时具有首选项的bean定义赋予属性primary=“true”。但是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.

在运行时,当您将获得自动连接类时,主true bean将获得自动连接的首选项。希望这能帮助你。欢呼。

#1


13  

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以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”)或@ resource (name=“dataTransferHandler”)插入正确的名称

#2


0  

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.

您可以为您希望在自动连接时具有首选项的bean定义赋予属性primary=“true”。但是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.

在运行时,当您将获得自动连接类时,主true bean将获得自动连接的首选项。希望这能帮助你。欢呼。