无法自动配置DataSource:未指定"spring.datasource.url"

Sub*_*h J 38 java spring mongodb spring-data-jpa spring-boot

我已经使用Web,MongoDB和JPA依赖项从SPRING INITIALIZR创建了一个基本的Spring启动应用程序.

当我尝试运行spring boot应用程序时,我收到以下异常:

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2018-03-25 16:27:02.807 ERROR 16256 --- [  restartedMain] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************
Description:
Failed to auto-configure a DataSource: 'spring.datasource.url' is not specified and no embedded datasource could be auto-configured.
Reason: Failed to determine a suitable driver class

Action:

Consider the following situation:
If you want an embedded database like H2, HSQL or Derby, please add it in the Classpath.
If you have database settings to be loaded from a particular profile you may need to activate it since no profiles were currently active.
Run Code Online (Sandbox Code Playgroud)

在application.properties文件中,我有以下配置:

server.port=8081
spring.data.mongodb.database=TestDatabase
spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
Run Code Online (Sandbox Code Playgroud)

我使用的版本: Spring:5.0.4,MongoDB:3.6,Spring Boot:2.0

小智 78

由于您在pom.xml文件中添加了mongodb和data-jpa依赖项,因此它创建了一个依赖项冲突,如下所示

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)

尝试删除jpa依赖项并运行.它应该工作正常.

  • 请注意,`spring-boot-starter-batch`也会引入jdbc的依赖项,从而激活同样的错误 (4认同)
  • 是的,这是我的问题.我删除了JPA依赖项,现在工作正常.谢谢. (2认同)

pus*_*dav 40

转到application.properties所在的资源文件夹,更新下面的代码.

spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
Run Code Online (Sandbox Code Playgroud)

  • 即使我没有在我的 pom.xml 中同时使用 jpa 和 mongodb 依赖项,我也遇到了这个问题……所以我得到了它,因为它是因为“spring-boot-starter-batch”依赖项。你能解释一下为什么这个设置有效吗? (2认同)

小智 18

在资源文件夹下的application.properties文件中添加以下行,然后重新启动应用程序.

spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
Run Code Online (Sandbox Code Playgroud)