Caused by: org.springframework.beans.factory.support.BeanDefinitionOverrideException: Invalid bean

这个问题整到 凌晨1:36,网上搜了很多个文章,解决方案都不对。有的都在乱说。

这类问题很多都是英文文章,没见说明白的,中文解决方案几乎没有。


另外也看到了类似的问题,其实这个问题就是重复扫描了。

# # 问题描述

Caused by: org.springframework.beans.factory.support.BeanDefinitionOverrideException: Invalid bean definition with name 'dataSource' defined in class path resource [org/apache/shardingsphere/shardingjdbc/spring/boot/SpringBootConfiguration.class]: Cannot register bean definition [Root bean: class [null]; scope=; abstract=false; lazyInit=null; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.apache.shardingsphere.shardingjdbc.spring.boot.SpringBootConfiguration; factoryMethodName=dataSource; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/apache/shardingsphere/shardingjdbc/spring/boot/SpringBootConfiguration.class]] for bean 'dataSource': There is already [Root bean: class [null]; scope=; abstract=false; lazyInit=null; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure; factoryMethodName=dataSource; initMethodName=init; destroyMethodName=(inferred); defined in class path resource [com/alibaba/druid/spring/boot/autoconfigure/DruidDataSourceAutoConfigure.class]] bound.

 

# # 报错场景

 正常的使用 Druid 数据连接池没有问题,我在使用springboot 整合 sharding-jdbc 的时候出现的这个错误。

 

# # 错误原因

  仔细分析,SpringBoot本身具有自动配置,现在自动配置时发生冲突了,那么我们可以将冲突的部分排除掉,即告诉SpringBoot,某个类不用帮我自动配置了,这里我们将Druid的关于数据源的配置排除掉。

 

# # 解决方案

  在主启动类上添加:

exclude={DruidDataSourceAutoConfigure.class}

 

 问题解决。

 

# # 其他的问题也是类似的

  只要看到红框里边的错误类型,导致的项目不能启动,按照上边的解决方式替换绿色框的内容即可。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章