Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Annotation-specified bean name conflicts with existing, non-compatible bean def

I'm having a problem with some Spring bean definitions. I have a couple of context xml files that are being loaded by my main() method, and both of them contain almost exclusively a tag. When my main method starts up, I get this error from Spring:

Caused by: org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name 'converterDAO' for bean class [my.package.InMemoryConverterDaoImpl] conflicts with existing, non-compatible bean definition of same name and class [my.other.package.StaticConverterDAOImpl] 

Both DAO classes are annotated this way:

@Repository("converterDAO") public class StaticConverterDAOImpl implements ConverterDAO { ... } 

The in-memory dao also has the @Repository("converterDAO") annotation. The dao is referenced in other classes like this:

... private @Autowired @Qualifier("converterDAO") ConverterDAO converterDAO; ... 

I want one DAO to override the definition of the other one, which as I always understood it was one of the principal reasons to use a DI framework in the first place. I've been doing this with xml definitions for years and never had any problems. But not so with component scans and annotated bean definitions? And what does Spring mean when it says they are not "compatible"? They implement the same interface, and they are autowired into fields that are of that interface type. Why the heck are they not compatible?

Can someone provide me with a way for one annotated, component-scanned bean to override another?

-Mike

like image 916
user1283068 Avatar asked Dec 10 '12 12:12

user1283068


1 Answers

I had a similar issue with Spring 4.x using @RestController. Two different packages had a class with the same name...

package com.x.catalog  @RestController public class TextureController { ...  package com.x.cms @RestController public class TextureController { ... 

The fix was easy...

package com.x.catalog  @RestController("CatalogTextureController") public class TextureController { ...  package com.x.cms @RestController("CMSTextureController") public class TextureController { ... 

The problem seems to be that the annotation gets autowired and takes the class name by default. Giving it an explicit name in the @RestController annotation allows you to keep the class names.

like image 165
Ian Newland Avatar answered Sep 28 '22 00:09

Ian Newland