【发布时间】:2018-06-04 22:55:10
【问题描述】:
我是 Spring boot 的新手。我收到此错误
Cannot determine embedded database driver class for database type NONE
每当尝试运行我的 spring-boot 启动 Web 应用程序时(我正在尝试测试执行器和 hal 浏览器)。在过去八小时左右的时间里,我尝试了一些关于 google/stackoverflow 的建议。但似乎对我不起作用。我仍然不断收到另一个错误。
第一次尝试: 我遵循journaldev中提到的两种方法
如果我使用 第一种方法,即注释我的主应用程序类
使用@EnableAutoConfiguration(exclude = { DataSourceAutoConfiguration.class }),我收到此错误:
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'javax.sql.DataSource' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
如果我使用第二种方法,我仍然会得到另一个错误:
Binding to target [Bindable@7c551ad4 type = com.zaxxer.hikari.HikariDataSource, value = 'provided', annotations = array<Annotation>[[empty]]] failed:
Property: driverclassname
Value: com.mysql.jdbc.Driver
Origin: "driverClassName" from property source "source"
Reason: Unable to set value for property driver-class-name
我还尝试了 Andy Wilkinson 的 suggestion 并添加了
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost/mydb
到我的 application.properties 文件,但我收到此错误:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: 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 java.lang.IllegalStateException: Cannot load driver class: com.mysql.jdbc.Driver
我还尝试提供用户名和密码(不确定是否需要,因为我没有尝试访问我的数据库),但对我不起作用。如果需要,我也可以提供我的 pom 配置。
【问题讨论】:
-
你的pom中有mysql依赖吗?
-
是的,我可以在有效的 pom.xml 中看到依赖关系。仅供参考,我使用的是 2.0.0.M3 版本的 spring-boot-starter-parent
-
我相信应该像这样指定驱动程序类名称
spring.datasource.driver-class-name= -
我仍然收到错误
Cannot load driver class: com.mysql.jdbc.Driver -
显然你需要一个
DataSource,因为你配置了一些需要它的东西。添加适当的数据源。您不能添加任意一个(就像您尝试使用 MySQL 驱动程序一样,因为它需要一个正在运行的 MySQL 实例并且需要适当的配置)。如果您不需要它,请删除需要数据库的依赖项(如 JPA 等),如果需要添加驱动程序(如内存数据库的 H2)或与您要连接的数据库匹配的驱动程序。
标签: spring spring-boot