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

 

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