SpringBoot - BeanDefinitionOverrideException: Invalid bean definition

前端 未结 4 969
情话喂你
情话喂你 2020-12-03 04:28

I am trying to setup DynamoDB locally with Spring Boot. Initially I got the setup working and was able to write/save to DynamoDB via a repository. From that point I added mo

4条回答
  •  一个人的身影
    2020-12-03 05:00

    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.

提交回复
热议问题