Spring boot unexpected duplicate bean - java

When I run spring boot I get the following error. I have replaced sensitive parts with '...' as the problem should be clear anyway. This error happened without me changing any of the code in the class that is causing the error.
Description:
The bean '...', defined in class path resource [....class], could not
be registered. A bean with that name has already been defined in URL
[jar:file:/C:/m2/repository/.../0-SNAPSHOT/...-0-SNAPSHOT.jar!/...class]
and overriding is disabled.
Action:
Consider renaming one of the beans or enabling overriding by setting
spring.main.allow-bean-definition-overriding=true

Related

The bean org.springframework.transaction.config.internalTransactionalEventListenerFactory could not be registered

"The bean org.springframework.transaction.config.internalTransactionalEventListenerFactory could not be registered. A bean with that name has already been defined and overriding is disabled."
After spring upgrade from 4.3.18 to 5.3.21 the above error has appeared.
Setting the spring.main.allow-bean-definition-overriding=true in properties file is not resolving the issue.

Occasional BeanCreationException in Java Spring Boot project built with Gradle

Sometimes I get a BeanCreationException when starting a Java Spring Boot single-JAR application built by Gradle. My investigations show that it depends on the JAR file and how it has been built by Gradle. Most often (but not always) I see the exception when I build the project on a Linux system. But I have never reproduced the issue with a JAR built on Windows or when running from IntelliJ Idea.
I tried to compare the content of a working JAR with the JAR throwing the exception - all the *.class files (including meta and resources) were binary equal, the only difference was in the order the files were stored in the JAR/ZIP archive. I also tried to unpack the failing JAR on Windows and just repack it into a new JAR file (using 7-zip) - the application started without any exceptions. This weird workaround solved the issue, but it's not something I'd like to do after each build on a linux machine.
The exception suggests to check circular references, so I tried replacing #Autowired properties with #Autowired bean constructors to help me to find the problem but that didn't help. The stacktrace does not mention any of my classes, so I don't know what bean could be responsible for the issue. And because of the fact the issue happens only sometimes and is solvable by repacking the JAR file, I'm not sure there are any circular references anyway.
Could you please help me? Any advice or suggestion is welcomed.
JDK: openjdk 8u262
Gradle version 5.6
Spring boot 2.3.1
Exception message:
BeanCreationException: Error creating bean with name 'webConfig':
Invocation of init method failed; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'requestMappingHandlerAdapter' defined in class path resource
[org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]:
Unsatisfied dependency expressed through method 'requestMappingHandlerAdapter' parameter 0;
nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException:
Error creating bean with name 'mvcContentNegotiationManager':
Requested bean is currently in creation: Is there an unresolvable circular reference?
By default, Spring manages itself bean's lifecycle and then, arranges their initialization order during the startup.
Here a full example of usage #DependsOn annotation to order the bean instanciation.
You can also found more detail in the official Spring documentation

Java Spring Boot Circular Dependency on one environment

I have a spring boot application which runs well on my local, UAT, and Prod environments but when I deploy it in a staging environment, it gives the following error:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'Bean 1': Unsatisfied dependency expressed through field 'Class 1'; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'Bean2 of type Class1': Bean with the name 'Bean2' has been injected into other beans [Bean 3] in its raw version as part of a circular reference, but has eventually been wrapped. This means that said other beans do not use the final version of the bean. This is often the result of over-eager type matching - consider using 'getBeanNamesOfType' with the 'allowEagerInit' flag turned off, for example.
I am not sure if it's related to the build process or what. I have checked the code, there are no circular dependencies. When I annotate Bean2 with #Lazy on Class3, it works well. But I am not able to figure out why it works on other environments and this annotation is needed only for the Staging.

BeanDefinitionOverrideException although Spring-Property overriding is enabled

Although the spring-property allow-bean-definition-overriding: true is enabled in the main application.yml, I am getting an Exception. Any clue why?
Application Start
***************************
APPLICATION FAILED TO START
***************************
Description:
The bean 'httpSessionManager', defined in class path resource [...], could not be registered. A bean with that name has already been defined in URL [...HttpSessionManager.class] and overriding is disabled.
Action:
Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true
Test Start
***************************
APPLICATION FAILED TO START
***************************
Description:
The bean 'metaDataSourceAdvisor' could not be registered. A bean with that name has already been defined and overriding is disabled.
Action:
Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true
If you look at the error message, it says:
Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true.
Make sure your allow-bean-definition-overriding property is under the spring.main element.

When I add Actuator dependency it gives UnsatisfiedDependencyException

I have a springboot application. When I add spring-boot-starter-actuator dependency to my pom it gives UnsatisfiedDependencyException.
Without this dependency, application is running. When I put it, application not running.
Can anyone provide a solution for this?
Error:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'servoMetricCollector' defined in class path resource

Categories