. Advertisement .
..3..
. Advertisement .
..4..
“BeanDefinitionOverrideException in Spring Boot” is one of the most common errors that shows up in many ways. What is the cause of it and how to fix it? Let’s read this article. We will explain this error clearly and provide some solutions below.
When do you get the error “BeanDefinitionOverrideException in Spring Boot“?
When running the program, you can easily get the following error:
BeanDefinitionOverrideException in Spring Boot
The BeanDefinitionOverrideException occurs when you construct two distinct Spring setups, each having a testBean() function. A disagreement between two separate testBeans is discovered. Additionally, the configuration classes TestConfiguration1 and TestConfiguration2 include the beans.
How to fix the error “BeanDefinitionOverrideException in Spring Boot“?
Solution 1: Include the property listed below
The simplest solution for you to solve the error “BeanDefinitionOverrideException in Spring Boot” is to include the property listed below in your test application. properties document:
/src/test/resources/application.properties
spring.main.allow-bean-definition-overriding=true
Then, let’s rebuild the program and it will run well without any errors.
Solution 2: Change method names
Another solution is to change method names. The names of the annotated methods are automatically used by Spring as bean names. The BeanDefinitionOverrideException will therefore not occur if you have beans declared in a configuration class, like in this case.
@Bean
public TestBean1 testBean1() {
return new TestBean1();
}
@Bean
public TestBean2 testBean2() {
return new TestBean2();
}
After doing that, your error will completely disappear.
Solution 3: Figure out which conflicting bean is related
There may occasionally be a name conflict brought on by beans coming from third-party spring-supported libraries. When this occurs, you should try to figure out which conflicting bean is related to your application in order to see whether any of the aforementioned fixes may be applied. After that, let’s configure Spring Boot to permit bean overriding to be a good solution if you are unable to change any of the bean definitions. Let’s set true for the spring.main.allow-bean-definition-overriding property in your application.properties file to enable bean overriding.
spring.main.allow-bean-definition-overriding=true
You are instructing Spring Boot to let bean overriding without altering bean definitions by doing this. As a last warning, you should be aware that it can be challenging to predict which bean will be created first because dependency connections, which are primarily controlled at runtime, govern the order in which beans are created. Because of this, permitting bean overriding may result in unexpected behavior unless you are sufficiently familiar with your beans’ dependence chain.
Solution 4: Duplicate MyClass
With MongoDB, we also experienced the similar issue. At least the error message was same, since we only had one MongoDB repository. It went something like this:
public interface MyClassMongoRepository extends MongoRepository<MyClass, Long> {
}
MyClass, a class that had previously been utilized in another database, was to blame for the issue. Before constructing the MongoRepository, Spring quietly constructed some JpaRepositories. Conflict arose because both repositories utilized the same name.
The solution is to duplicate MyClass, put it in the MongoRepository’s package, and get rid of any JPA-specific annotations.
After doing that, the error will completely disappear and the program will run well. So, let’s apply this method to get your desired results.
Conclusion
“BeanDefinitionOverrideException in Spring Boot” is a confusing problem. Above is some best solutions we suggest. We believe that, with these methods you can easily fix your problem. If you have any other questions or concerns, please leave a comment below. Thank you for reading; we are always excited when one of our posts can provide useful information on a topic like this!
Read more
Leave a comment