SpringBoot - BeanDefinitionOverrideException: Invalid bean definition

Solution 1:

Bean overriding has to be enabled since Spring Boot 2.1,

https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.1-Release-Notes

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")

Solution 3:

I think I had the same problem with MongoDB. At least the error message looked exactly the same and I also had just one repository for the MongoDB, something like this:

public interface MyClassMongoRepository extends MongoRepository<MyClass, Long> {
}

The problem had been caused by class MyClass which had been used in another database before. Spring silently created some JpaRepository before creating the MongoRepository. Both repositories had the same name which caused the conflict.

Solution was to make a copy of MyClass, move it into the package of the MongoRepository and remove any JPA-specific annotations.

Solution 4:

I just stumbled across the same issue while trying to add a PostgreSQL database via spring-data-jdbc to an existing project wich was already using a MongoDB.

It seems like the problem was that the repositories for MongoDB and PostgreSQL were scanned by both modules (spring-mongo and spring-jdbc). They both try to create some beans and clash.

In my case the MongoDB repositories and the PostgreSQL repositories were in the same package.

The accepted answer solved the problem for me - but i kind of got a hint from this startup logs:

Finished Spring Data repository scanning in 319ms. Found 4 repository interfaces
Finished Spring Data repository scanning in 319ms. Found 5 repository interfaces

This is weird because i only have 1 repository for PostgreSQL and 4 for MongoDB.

I moved the PostgreSQL repository into a different package than the MongoDB repository and configured the base package of the PostgreSQL repositories to the new package. In my case:

@EnableJdbcRepositories(basePackageClasses = MyOnlyPostgreSQLRepository.class) // TODO: Use the real package or a dedicated base class

This solved the issue for me (no property set for bean overriding - which i prefer). The startups logs also show the correct amount of repositories now (1 and 4).