添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

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

package com.app;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class BeanOne {
    @Bean
    public String sampleBean() {
        return "This is Bean One";

BeanTwo.java

package com.app;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class BeanTwo {
    @Bean
    public String sampleBean() {
        return "This is Bean Two";


Log File:

Now we need to write the allow-bean-definition-overriding property to true in our application.properties file.

Fixing the BeanDefinitionOverrideException

To fix this BeanDefinitionOverrideException in Spring Boot, we need to change the bean names in any of them. As you can see in the code above, there are two configuration classes with the same bean names. Now we change the bean name in the BeanTwo configuration class. Then this BeanDefinitionOverrideException disappears.

BeanOne.java

package com.app;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class BeanOne {
    @Bean
    public String sampleBean() {
        return "This is Bean One";

BeanTwo.java

Spring - Inheriting Bean
In Spring, bean inheritance allows for reusing configuration from a parent bean and customizing it in child beans. This feature helps in reducing redundancy and managing configurations more efficiently. Unlike Java class inheritance, Spring bean inheritance is more about configuration inheritance ra
4 min read
Override default Spring Boot properties in Junit Test
The Spring Boot framework provides a lot of features for testing our applications. Here, we have a tool called JUnit, which is used to test software applications. We can override default Spring Boot properties in JUnit tests, allowing us to customize the behavior of the Spring Boot Application for s
5 min read
Spring Boot - Dependency Injection and Spring Beans
Spring Boot is a powerful framework for building RESTful APIs and microservices with minimal configuration. Two fundamental concepts within Spring Boot are Dependency Injection (DI) and Spring Beans. Dependency Injection is a design pattern used to implement Inversion of Control (IoC), allowing the
6 min read
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 !