天天看点

springBoot Error creating bean with name 'dataSource' defined in class path resource

  • springboot前端服务,没有操作数据库,报dataSource异常
Error creating bean with name 'dataSource' defined in class path resource
[org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Bean
instantiation via factory method failed; nested exception is
org.springframework.beans.BeanInstantiationException: Failed to instantiate
[com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception; nested
exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to determine a suitable driver class
           
  • 原因
  1. spring boot会默认加载org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration类
  2. DataSourceAutoConfiguration又使用了@Configuration注解向spring注入了dataSource bean
  3. 但是因为工程中没有关于dataSource相关的配置信息,当spring创建dataSource bean因缺少相关的信息就会报错
  • 解决

启动工程时排除:DataSourceAutoConfiguration即可

如在@SpringBootApplication启动注解上加 exclude={DataSourceAutoConfiguration.class}

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
           
bug

继续阅读