Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm using Spring 2.5.4 and am creating a Java application that I'm deploying onto Weblogic.

I have a class in an external library (which included in the WEB-INF/classes directory of the resulting WAR file of my application) that I want to use in my code. I've created an instance variable for an object of the class in my code and added the @Autowired annotation and a getter and setter. In my application context file I have declared a bean of the library class' type and added the following:

<context:annotation-config />
<context:component-scan base-package="com.mycompany" />

... in order to register an AutowiredAnnotationBeanPostProcessor that will scan the classes and process the annotation.

When I try and deploy the application, I get the following error:

java.lang.IllegalStateException: Annotation-specified bean name 'myBean' for bean
class [com.mycompany.package.ClassName] conflicts with existing, non-compatible
bean definition of same name and class [com.mycompany.otherPackage.ClassName]

I think this is because there's a class in the library which has the same name as one in my application code (both class' package names start with "com.mycompany"). Nb. this is NOT the class that I have added, but a different one. Is there any way I can circumvent this problem without changing the name of the class in my application?

Thanks for any assistance.

like image 539
GarlicBread Avatar asked Dec 01 '22 21:12

GarlicBread


1 Answers

Old question but throwing my 2c of bad experience with similar problem. If you have 2 classes with same name, but in different packages was there a time when you had your other class referenced by the failing Spring context? If so, I'd recommend to clean the AS cached files (typically the place where the WAR is extracted), clean/rebuild your WAR and deploy again. Restarting the app server is also recommended. I found that application servers and web containers alike (Weblogic, WAS, Jboss, Tomcat) tend to leave behind the old classes and when application is deployed those stale .class files are loaded in JVM via some old references, which most of the time messes up the Spring context loader.

Typical scenario is when you have renamed/moved a class from one package to another, or even kept the package name the same but moved it to another module (jar). In such cases cached (left over) files in the AS work directory can cause big headaches. Wiping out the work directory in your AS should resolve the issue outright.

like image 78
Pavel Lechev Avatar answered Dec 21 '22 14:12

Pavel Lechev