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 beanclass [com.mycompany.package.ClassName] conflicts with existing, non-compatiblebean 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.

4

Best Answer


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.

You should use @qualifier to avoid this kind of conflict please refer section 3.9.3.

I fixed the problem by removing the autowiring completely and accessing the bean by explicitly creating a reference to it through the application context and the getBean() method.

This would better fit as a comment to @Pavel Lechev's answer, but I don't have enough rep to comment yet.

For other's finding this, here's what I did to solve this problem. I am using Wildfly 9.0.2.Final and, IntelliJ IDEA 2016.1.3 Build #IU-145.1617. These steps should presumably work with JBoss as well.

  1. Stop Wildfly server.
  2. Navigate to $WILDFLY_HOME/standalone/. Delete the three following folders: lib/, log/ and temp/.
  3. In IntelliJ, Build > Build Artifacts > All Artifacts > Clean (or just the artifacts you are deploying).
  4. In IntelliJ, Build > Rebuild Project
  5. Restart Wildfly and redeploy your artifact(s).

These steps remedied my issue of duplicate bean names detected in the Spring context after refactoring a package name upstream from a couple of Controllers.