无法自动配置数据源:未指定“spring.datasource.url”

我从SPRING INITIALIZR创建了一个基本的弹簧引导应用程序,其中包含Web,MongoDB和JPA依赖项。

当我尝试运行弹簧启动应用程序时,我得到以下异常:

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.

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

server.port=8081
spring.data.mongodb.database=TestDatabase
spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017

我使用的版本:弹簧 : 5.0.4, MongoDB : 3.6, 弹簧靴: 2.0


答案 1

由于您在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>

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


答案 2

转到存在 application.properties 的资源文件夹,更新其中的以下代码。

spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration