ConflictingBeanDefinitionException even after deleting a class - java

I am developing a simple maven + spring application and i am getting the following error. It says two of my classes have a conflict. so i deleted the second class but i am still getting the same error. I tried restarting the server but it still says my class exists. Can somebody help?
Exception while loading the app : java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [com.nibm.config.RootConfig]; nested exception is org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name 'employeeController' for bean class [com.nibm.hibernate.controller.EmployeeController] conflicts with existing, non-compatible bean definition of same name and class [com.nibm.controller.EmployeeController]

I was able to solve a similar problem by using IntelliJ's function "Rebuild".
The reason was an orphan .class file after the corresponding .java file had already been deleted.

You get the exception because you have two spring beans of the same class.
This exception is thrown by
org.springframework.context.annotation.ClassPathBeanDefinitionScanner#isCompatible
And looking at that implementation it looks as if you create a bean of type EmployeeController in your RootConfig and additionally by ComponentScan.
To fix the problem remove the bean from the RootConfig or change your ComponentScan, so this bean is not found by it.
You can set a breakpoint in the constructor of EmployeeController. From the stack you can get more information about how and why the bean is created.

I faced the same problem and it was because class with same name exist at two locations as mentioned in the Exception itself which are conflicting and after removing one issue got fixed.

I was getting this same ConflictingBeanDefinitionException..."conflicts with existing, non-compatible bean definition of same name and class" when running JUnit tests with #RunWith(SpringRunner.class)/ #SpringBootTest from inside Intellij.
Execution via gradle:build of the same tests were running fine.
This began to happen after I had refactored the packaging of several #Components which lead me to believe something was holding a reference to the class under it's previous package name.
No amount of gradle build/clean would seem to clear it.
Doing a Build -> Rebuild Project in IntelliJ was what cleared this issue for me.

Related

jhipster java tests failing - application properties

I have generated a jhipster monolithic app. I have created a class to connect with the AWS S3 and upload a file there. I defined the properties in .yml file. Here everything works fine.
When I am trying to run the provided tests, most of them are failing with the following error:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 's3AutoConfig': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'aws.endpoint.url' in value "${aws.endpoint.url}"
S3AutoConfig is the class which uses the properties.
I checked jhipster's documenation and several posts, like the one below:
Adding applicationproperties in Jhipster
which mention that you should provide the properties in the ApplicationProperties class (seems a bit redundant).
I defined the properties also in the java class, but the tests are still failing with the same error above.
How should I define the properties, so they are picked up by the tests? Is it necessary to provide them also in the java class as some posts suggest?
Your implementation cannot work because you are defining Aws class within ApplicationProperties which means that your AWS properties will be prefixed by application, so for example application.aws.endpoint.url which does not match your application*.yml structure and this is why you get this error.
You should extract Aws class and its inner classes to its own file (Aws.java) and use prefix "aws".
Also, it would probably better named as AwsProperties.
#ConfigurationProperties(prefix = "aws", ignoreUnknownFields = false)
public class Aws {
Then the second point about tests is that they are using a different classpath than main class so you should ensure that you define these properties also in src/test/resources/config/application.yml

Spring Boot 2 Upgrade not able to resolve class name as property

We have an application where one of our internal libraries has defined a bean like this
<bean id="myBean" class="${myBean.type}"/>
We have a Spring Cloud Config Server which feeds properties to this application on startup, which also contains the property myBean.type. This setup is currently working fine with no issues. I then made the following change to my pom
Earlier
<spring.boot.version>1.5.16.RELEASE</spring.boot.version>
<spring.cloud.version>Edgware.RELEASE</spring.cloud.version>
<spring.version>4.3.19.RELEASE</spring.version>
Now
<spring.boot.version>2.0.9.RELEASE</spring.boot.version>
<spring.version>5.0.13.RELEASE</spring.version>
<spring.cloud.version>Finchley.SR2</spring.cloud.version>
Then I started getting this error on startup
An attempt was made to call the method org.springframework.beans.factory.support.BeanDefinitionBuilder.addConstructorArg(Ljava/lang/Object;)Lorg/springframework/beans/factory/support/BeanDefinitionBuilder; but it does not exist. Its class, org.springframework.beans.factory.support.BeanDefinitionBuilder, is available from the following locations:
jar:file:/I:/Library/MavenRepository/org/springframework/spring-beans/5.0.13.RELEASE/spring-beans-5.0.13.RELEASE.jar!/org/springframework/beans/factory/support/BeanDefinitionBuilder.class
It was loaded from the following location:
file:/I:/Library/MavenRepository/org/springframework/spring-beans/5.0.13.RELEASE/spring-beans-5.0.13.RELEASE.jar
Action:
Correct the classpath of your application so that it contains a single, compatible version of org.springframework.beans.factory.support.BeanDefinitionBuilder
Looking at this, I excluded the artifact org.apache.cxf:cxf-api:jar:2.7.18:compile from all the jars that depended on it & upgraded cxf jars versions to 3.2.5. Now the startup is going ahead but it is giving me the following error:
org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [${myBean.type}] for bean with name 'myBean' defined in class path resource [xyz.xml]; nested exception is java.lang.ClassNotFoundException: ${myBean.type}
I am not sure if removal of cxf-api is causing this issue, or upgrade of Spring Boot 2, or is it something else that is going wrong here!

ambiguous mapping while deploying

I'm using Jenkins 1.6.20 (Git Client Plugin 1.18.0, Git Plugin 2.4.0) to get the Java application code from bitbucket.org and deploy it to Apache Tomcat 8.0.23.
The error appears while deploying and looks like:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestMappingHandlerMapping' defined in class path resource [org/spr$
public java.util.List by.ipps.accounting.ws.PositionWS.getEmployeePost(java.lang.Long)
to {[/positionListJson/{id}],methods=[GET],params=[],headers=[],consumes=[],produces=[application/json],custom=[]}: There is already 'resourceWS' bean method
public by.ipps.accounting.model.Employee.EmployeePost by.ipps.accounting.ws.ResourceWS.getEmployeePost(java.lang.Long) mapped.
bla-bla-bla ... so many errors ...
Caused by: java.lang.IllegalStateException: Ambiguous mapping found. Cannot map 'positionWS' bean method
public java.util.List by.ipps.accounting.ws.PositionWS.getEmployeePost(java.lang.Long)
to {[/positionListJson/{id}],methods=[GET],params=[],headers=[],consumes=[],produces=[application/json],custom=[]}: There is already 'resourceWS' bean method
public by.ipps.accounting.model.Employee.EmployeePost by.ipps.accounting.ws.ResourceWS.getEmployeePost(java.lang.Long) mapped.
The problem is that the class (with annotation #Controller) PositionWS with method getEmployeePost was renamed to ResourceWS a week ago, so exists no more, so I should not get this error.
To fix this I have to create a blank PositionWS controller (with no methods in it), commit & push that to bitbucket (and delete (cus i really don't need it) later and commit & push).
It seems to be like a bug in any of the applications I use. I can't find out in which app there is a bug to report it. Tell me please, if anyone faced such problems.
The heart of the issue was in incorrect configuration of Jenkins, it was my fault.
When I was configuring Jenkins I set maven goal as "install", but it must be "clean install". According to this Jenkins never deleted old files and kept them, so got a lot of issues of different kinds and with different log messages.
Due to Jenkins working specialty it downloads project files and try to assemble it on path /var/lib/jenkins/jobs/<projectName>/workspace/target/.
So I've drop the data in this folder and afterwards set maven goal to "clean install" and that fixed the issue.

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.
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.
Stop Wildfly server.
Navigate to $WILDFLY_HOME/standalone/. Delete the three following folders: lib/, log/ and temp/.
In IntelliJ, Build > Build Artifacts > All Artifacts > Clean (or just the artifacts you are deploying).
In IntelliJ, Build > Rebuild Project
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.

Class path resource not found randomly

We have integrated Spring with Hibernate and Struts2 in Eclipse and have been getting these errors when running the build:
14-Oct-2011 10:37:27 org.apache.catalina.core.StandardContext listenerStart
SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Invocation of init method failed; nested exception is java.io.FileNotFoundException: class path resource [Test.hbm.xml] cannot be opened because it does not exist
...
Caused by: java.io.FileNotFoundException: class path resource [Test.hbm.xml] cannot be opened because it does not exist
...
Our mapping in applicationContext.xml is:
<property name="mappingResources" value="Test.hbm.xml" />
Test.hbm.xml is in /build/classes, and we have tried putting it in /WEB-INF, /src, and even created a new source folder called /hibernate and dumped it in and it worked on only a few machines.
The first time we wrote it, it showed this error. And then we copied the exact same code into a new project, it suddenly worked. On some machines, it works; on others, it shows this error.
Does anyone know what could be wrong? Let me know if any other information is needed.
Thanks so much.
That's not a build-time error. It's a run-time one. Intermittent failures of that sort indicate a problem with your build, though. As a classpath resource, Test.hbm.xml must be in a classpath location when you run your app. Where exactly that is depends on how you're running it, but /WEB-INF will never work for a webapp, and a random /hibernate folder is unlikely to work, too, unless you've done some custom configuration to make that a classpath resource or make it build to one. For a more definitive answer, you'll need to provide more detail about what you're running and how you're building it.

Categories

Resources