运行 Spring Boot 应用程序时出现数据源错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47938922/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
DataSource Error while running Spring Boot application
提问by Asif Kamran Malick
I am newbie in Spring boot.I get this error
我是 Spring Boot 的新手。我收到此错误
Cannot determine embedded database driver class for database type NONE
Cannot determine embedded database driver class for database type NONE
whenever trying to run my spring-boot start web app(I am trying to test the actuator and hal browser). Over the last eight hours or so I have tryied several suggestions over google/stackoverflow. But doesn't seem to work for me. I still keep getting another error.
每当尝试运行我的 spring-boot start web 应用程序时(我正在尝试测试执行器和 hal 浏览器)。在过去八个小时左右的时间里,我尝试了一些关于 google/stackoverflow 的建议。但似乎对我不起作用。我仍然不断收到另一个错误。
First try:I followed both the methods mentioned in journaldev
第一次尝试:我遵循了journaldev 中提到的两种方法
If I use the first methodi.e. annotating my main application class
with @EnableAutoConfiguration(exclude = { DataSourceAutoConfiguration.class }), I get this error:
如果我使用第一种方法,即用 注释我的主应用程序类@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: {}
If I use the second methodwhich, I still get another error:
如果我使用第二种方法,我仍然会收到另一个错误:
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
I also tried Andy Wilkinson'ssuggestionand added
我也尝试了安迪威尔金森的建议并补充说
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost/mydb
to my application.propertiesfile but I got this error :
到我的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
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
I also tried with providing the username and pwd(not sure if that's required as I am not trying to access my database), but didn't work for me. If it's reqwuired I can provide my pom configurations too.
我还尝试提供用户名和密码(不确定是否需要,因为我没有尝试访问我的数据库),但对我不起作用。如果需要,我也可以提供我的 pom 配置。
回答by Atul
Below configuration is working perfectly fine for me -
以下配置对我来说非常好 -
application.properties -
application.properties -
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/rolb
spring.datasource.username=root
spring.datasource.password=root123
spring.datasource.initialize=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
pom.xml -
pom.xml -
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- <dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency> -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
If you want , you can also download source for my example application to compare - https://github.com/atulajoshi24/springboot-rest.git
如果需要,您还可以下载我的示例应用程序的源代码进行比较 - https://github.com/atulajoshi24/springboot-rest.git
The related blog post for the same - http://thejavatechie.com/2017/12/21/single-page-application-using-spring-boot-rest-and-angular-1-part-1/
相同的相关博客文章 - http://thejavatechie.com/2017/12/21/single-page-application-using-spring-boot-rest-and-angular-1-part-1/
回答by Janar
You said you don't need to access database so you should be able to use
你说你不需要访问数据库所以你应该可以使用
@EnableAutoConfiguration(exclude = { DataSourceAutoConfiguration.class })
and removing all autowirings which include datasource. The exception you got
并删除所有包含数据源的自动装配。你得到的例外
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: {}
says that you are trying to autowire datasource somewhere but you don't have one configured (since you excluded it). Just remove the autowired datasource and it should work.
说你正试图在某处自动装配数据源,但你没有配置(因为你排除了它)。只需删除自动装配的数据源,它应该可以工作。
If you do need to use database, then there seems to be a problem with mysql driver - make sure you have one added as dependency.
如果您确实需要使用数据库,那么 mysql 驱动程序似乎有问题 - 确保您添加了一个作为依赖项。
回答by Zubair A.
You need to add JDBC DRIVER dependency in pom file, and then should work
您需要在 pom 文件中添加 JDBC DRIVER 依赖项,然后应该可以工作

