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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49475177/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 03:01:16  来源:igfitidea点击:

Failed to auto-configure a DataSource: 'spring.datasource.url' is not specified

javaspringmongodbspring-bootspring-data-jpa

提问by Subash J

I have created a basic spring boot application from SPRING INITIALIZRwith the Web, MongoDB and JPA dependencies.

我已经从带有 Web、MongoDB 和 JPA 依赖项的SPRING INITIALIZR创建了一个基本的 Spring Boot 应用程序。

When I try to run the spring boot application I am getting the following exception:

当我尝试运行 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.

In application.properties file I am having the following configuration:

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

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

Versions which I use:Spring : 5.0.4, MongoDB : 3.6, Spring Boot: 2.0

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

采纳答案by Bhabadyuti

Since you have added both mongodb and data-jpa dependencies in your pom.xml file, it was creating a dependency conflict like below

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

Try removing jpa dependency and run. It should work fine.

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

回答by Nikolas

Seems there is missing MongoDB driver. Include the following dependency to pom.xml:

似乎缺少 MongoDB 驱动程序。包括以下依赖项pom.xml

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>

回答by pushpendra yadav

Go to resources folder where the application.properties is present, update the below code in that.

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

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

回答by bikram sahoo

Add the line below in application.propertiesfile under resource folder and restart your application.

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

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

回答by German Andres Ariza Casta?eda

your dependency based on data is trying to find their respective entities which one has not been created, comments the dependencies based on data and runs the app again.

您基于数据的依赖项试图找到它们各自尚未创建的实体,根据数据注释依赖项并再次运行应用程序。

<!-- <dependency> -->
        <!-- <groupId>org.springframework.boot</groupId> -->
        <!-- <artifactId>spring-boot-starter-data-jpa</artifactId> -->
        <!-- </dependency> -->

回答by CHANDU DIATOZ

Add your dependencies like mongodb,web,jpa. Delete/clear the remainings.

添加您的依赖项,如 mongodb、web、jpa。删除/清除剩余部分。

<dependencies>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-mongodb</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
</dependencies>

回答by Lava Kumar

adding org.apache.derby dependency solved my issue.

添加 org.apache.derby 依赖项解决了我的问题。

<dependency>
            <groupId>org.apache.derby</groupId>
            <artifactId>derby</artifactId>
            <scope>runtime</scope>
        </dependency>

回答by ngPranav

In gradle build i simply:

在 gradle 构建中,我只是:

compile('org.springframework.boot:spring-boot-starter-data-jpa') compile('org.springframework.boot:spring-boot-starter-security') compile('org.springframework.boot:spring-boot-starter-web') compile('org.springframework.boot:spring-boot-devtools')

compile('org.springframework.boot:spring-boot-starter-data-jpa') compile('org.springframework.boot:spring-boot-starter-security') compile('org.springframework.boot:spring-boot-starter-web') compile('org.springframework.boot:spring-boot-devtools')

removed

移除

**`compile('org.springframework.boot:spring-boot-starter-data-jpa')`**

and it worked for me.

它对我有用。

回答by Atul

This error occurred when you are putting JPA dependencies in your spring-boot configuration file like in maven or gradle. The solution is: Spring-Boot Documentation

当您将 JPA 依赖项放入您的 spring-boot 配置文件(如 maven 或 gradle)时,会发生此错误。解决方法是:Spring-Boot 文档

You have to specify the DB connection string and driver details in application.properties file. This will solve the issue. This might help to someone.

您必须在 application.properties 文件中指定数据库连接字符串和驱动程序详细信息。这将解决问题。这可能对某人有所帮助。

回答by Prashant Saraswat

I ran into this problem when I simply mistyped my jdbc url in application.properties. Hope this helps someone: before:

当我在 application.properties 中错误输入 jdbc url 时,我遇到了这个问题。希望这有助于某人:之前:

spring.datasource.url=jdbc://localhost:3306/test

after:

后:

spring.datasource.url=jdbc:mysql://localhost:3306/test