close

[Solved] SpringBoot – BeanDefinitionOverrideException: Invalid bean definition

Hello Guys, How are you all? Hope You all Are Fine. Today I am facing the following error SpringBoot – BeanDefinitionOverrideException: Invalid bean definition in Java. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How SpringBoot – BeanDefinitionOverrideException: Invalid bean definition Error Occurs?

Today I am facing the following error SpringBoot – BeanDefinitionOverrideException: Invalid bean definition in Java.

How To Solve SpringBoot – BeanDefinitionOverrideException: Invalid bean definition Error ?

  1. How To Solve SpringBoot – BeanDefinitionOverrideException: Invalid bean definition Error ?

    To Solve SpringBoot – BeanDefinitionOverrideException: Invalid bean definition Error Will cause this error in > 2.1, by default bean names are taken from the method name. Renaming the method or adding the name attribute to the Bean annotation will be a valid fix.

  2. SpringBoot – BeanDefinitionOverrideException: Invalid bean definition

    To Solve SpringBoot – BeanDefinitionOverrideException: Invalid bean definition Error Will cause this error in > 2.1, by default bean names are taken from the method name. Renaming the method or adding the name attribute to the Bean annotation will be a valid fix.

Solution 1

Bean overriding has to be enabled since Spring Boot 2.1,

Bean Overriding

Bean overriding has been disabled by default to prevent a bean being accidentally overridden. If you are relying on overriding, you will need to set spring.main.allow-bean-definition-overriding to true.

Set

spring.main.allow-bean-definition-overriding=true

or yml,

spring:
   main:
     allow-bean-definition-overriding: true

to enable overriding again.

Edit,

Bean Overriding is based of the name of the bean not its type. e.g.

@Bean
public ClassA class(){
   return new ClassA();
}

@Bean
public ClassB class(){
   return new ClassB();
}

Will cause this error in > 2.1, by default bean names are taken from the method name. Renaming the method or adding the name attribute to the Bean annotation will be a valid fix.

Solution 2

Enable bean overriding with such approach for example

@SpringBootTest(properties = "spring.main.allow-bean-definition-overriding=true")

or

@SpringBootApplication (properties = "spring.main.allow-bean-definition-overriding=true")

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read