Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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]
like image 553
Marco Avatar asked Jun 13 '12 12:06

Marco


1 Answers

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.

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")

like image 165
Biju Kunjummen Avatar answered Oct 11 '22 09:10

Biju Kunjummen