- 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
- 原因
- spring boot會默認加載org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration類
- DataSourceAutoConfiguration又使用了@Configuration註解向spring注入了dataSource bean
- 但是因爲工程中沒有關於dataSource相關的配置信息,當spring創建dataSource bean因缺少相關的信息就會報錯
- 解決
啓動工程時排除:DataSourceAutoConfiguration即可
如在@SpringBootApplication啓動註解上加 exclude={DataSourceAutoConfiguration.class}
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})