The
BeanDefinitionOverrideException
is a type of exception in Spring Boot. This occurs in Spring Boot when we try to override a bean definition that already exists in our application. If we have two beans with the same name, Spring throws an exception to prevent developers from overriding the existing bean. If we want to override a bean, we need to write the
allow-bean-definition-overriding
property to true
in our
application.properties
file. This allows us to override a bean in a Spring application.
In this article, we explain how to troubleshoot the
BeanDefinitionOverrideException
in Spring Boot with sample examples.
Reasons for BeanDefinitionOverrideException:
-
We have multiple configuration classes and are defining beans with the same name.
-
We have a bean with the same name in our code and an external library.
-
We accidentally redefine the same bean name in our Spring application context.
Troubleshooting BeanDefinitionOverrideException:
-
Use different bean names in the application context
-
Avoid Unintended Bean Scans
-
Use Conditional Annotations
-
Enable Bean Overriding
-
Use stereotype annotations
-
Use the @Autowired annotation to inject beans into your classes then It will help spring to resolve bean dependency.
-
Use @Qualifier annotation to specify the name of the bean that you want to inject.
Prerequisites:
-
Spring Framework
-
Spring Auto Configuration
-
Beans in Spring
-
Spring Annotations
-
Spring Dependency Management System
Project Folder Structure:
After creating the Spring Boot Project, the folder structure will look like below:
Example For BeanDefinitionOverrideException in Spring Boot
Below, we provide a sample example to demonstrate the BeanDefinitionOverrideException in Spring Boot when we use two beans with the same names. Here, we created two configuration classes and defined two beans in those classes with the same name. Then, we run the Spring Application, and we encounter this BeanDefinitionOverrideException.
BeanOne.java
How to Create Custom Banners in Spring Boot?
Custom banners in Spring Boot allow us to add a creative touch to the startup of our Spring Boot application. By default, Spring Boot displays a simple banner on the console when the application starts. We can also replace this with a custom banner. To create a custom banner in Spring Boot, we can s
3 min read
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy
&
Privacy Policy
Cookies are not collected in the GeeksforGeeks mobile applications.
Got It !