配置数据源失败:未指定'url'属性,也无法配置嵌入式数据源。

340

我正在使用MongoDB和Spring Boot Batch进行开发,已经启动了mongod服务器。

当我启动应用程序时,遇到了以下错误。

有什么建议可以解决这个问题吗?

***************************
APPLICATION FAILED TO START
***************************

Description:

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

Reason: Failed to determine a suitable driver class


Action:

Consider the following:
    If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
    If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).

应用程序配置文件:

# Mongo database URI. Cannot be set with host, port and credentials.
spring.data.mongodb.uri=mongodb://localhost/test 

pom.xml

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

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

我已经使用以下输出启动mongod

C:\Users\pc>mongod
2018-07-07T14:39:39.223+0530 I JOURNAL  [initandlisten] journal dir=C:\data\db\journal
2018-07-07T14:39:39.230+0530 I JOURNAL  [initandlisten] recover : no journal files present, no recovery needed
2018-07-07T14:39:39.478+0530 I JOURNAL  [durability] Durability thread started
2018-07-07T14:39:39.589+0530 I CONTROL  [initandlisten] MongoDB starting : pid=11992 port=27017 dbpath=C:\data\db\ 64-bit host=DESKTOP-NQ639DU
2018-07-07T14:39:39.589+0530 I CONTROL  [initandlisten] targetMinOS: Windows 7/Windows Server 2008 R2
2018-07-07T14:39:39.591+0530 I CONTROL  [initandlisten] db version v3.0.5
2018-07-07T14:39:39.592+0530 I CONTROL  [initandlisten] git version: 8bc4ae20708dbb493cb09338d9e7be6698e4a3a3
2018-07-07T14:39:39.592+0530 I CONTROL  [initandlisten] build info: windows sys.getwindowsversion(major=6, minor=1, build=7601, platform=2, service_pack='Service Pack 1') BOOST_LIB_VERSION=1_49
2018-07-07T14:39:39.592+0530 I CONTROL  [initandlisten] allocator: tcmalloc
2018-07-07T14:39:39.593+0530 I CONTROL  [initandlisten] options: {}
2018-07-07T14:39:39.595+0530 I JOURNAL  [journal writer] Journal writer thread started
2018-07-07T14:39:40.485+0530 I NETWORK  [initandlisten] waiting for connections on port 27017
2018-07-07T14:40:39.140+0530 I NETWORK  [initandlisten] connection accepted from 127.0.0.1:51340 #1 (1 connection now open)
2018-07-07T14:40:41.663+0530 I NETWORK  [conn1] end connection 127.0.0.1:51340 (0 connections now open)
2018-07-07T14:45:12.421+0530 I NETWORK  [initandlisten] connection accepted from 127.0.0.1:51578 #2 (1 connection now open)
2018-07-07T14:45:12.870+0530 I NETWORK  [conn2] end connection 127.0.0.1:51578 (0 connections now open)
2018-07-07T14:46:21.734+0530 I NETWORK  [initandlisten] connection accepted from 127.0.0.1:51591 #3 (1 connection now open)
2018-07-07T14:46:22.041+0530 I NETWORK  [conn3] end connection 127.0.0.1:51591 (0 connections now open)
2018-07-07T14:57:47.523+0530 I NETWORK  [initandlisten] connection accepted from 127.0.0.1:52534 #4 (1 connection now open)
2018-07-07T14:57:47.910+0530 I NETWORK  [conn4] end connection 127.0.0.1:52534 (0 connections now open)

enter image description here


对于那些遇到这种情况的人 - 有时候我们会忘记在修改pom.xml后重新安装maven(mvn clean install)。所以如果你感觉出现了意外情况,也可以尝试这样做。 - lingar
50个回答

317

只需添加:@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class })即可解决问题。

我遇到相同的错误,尝试使用@EnableAutoConfiguration(exclude=...)没有起作用。

对于那些想知道在哪里添加@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class })的人,正如用户所问。您需要将其添加到位于src>main>java下的主应用程序类中。默认情况下它被设置为@SpringBootApplication


5
当我知道我需要在某个时间点使用数据源但不需要立即使用它时,这个功能完美地发挥作用。 - alex
15
这确实起作用,但错误的原因是向pom.xml添加了数据库依赖项,但没有添加连接到数据库的变量。// 如果您想连接到数据库,必须在application.properties中适当的位置添加属性。 - Tanakorn Lueangkajonvit
是的,这正是我在寻找的解决方案,因为我的应用程序目前还没有任何数据钩子。 - Wadi Diaz-wong
5
在 Kotlin 中翻译 @SpringBootApplication(exclude = [DataSourceAutoConfiguration::class])@SpringBootApplication(exclude = [DataSourceAutoConfiguration :: class]) - Sami
非常适用于spring-boot-starter-data-mongodb。 - Ruslan López
显示剩余7条评论

190

请检查您的application.properties文件

更改中

spring.datasource.driverClassName=com.mysql.jdbc.Driver

spring.datasource.driver-class-name=com.mysql.jdbc.Driver

对我很有用。完整配置:

spring.datasource.url=jdbc:mysql://localhost:3306/db
spring.datasource.username=
spring.datasource.password=   
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.database-platform = org.hibernate.dialect.MySQL5Dialect
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto = update

16
@GhostDede 请检查 src/main/resources/application.properties 文件。 - kayesh parvez
谢谢!它帮助我弄清楚了我的代码在做什么。 - Saxophonist
5
如果这个驱动程序:"com.mysql.jdbc.Driver"被弃用,考虑使用这个:"com.mysql.cj.jdbc.Driver"。这对我起作用了! - Miguel Rodríguez
org.springframework.beans.factory.UnsatisfiedDependencyException: 在类路径资源[org/springframework/boot/devtools/autoconfigure/DevToolsDataSourceAutoConfiguration.class]中定义的名为'inMemoryDatabaseShutdownExecutor'的bean创建错误:通过方法'inMemoryDatabaseShutdownExecutor'参数0表达的不满足依赖关系;嵌套异常是org.springframework.beans.factory.BeanCreationException: - Satish Hawalppagol
正如@saurabhshcs在下面提到的,您也可以使用org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration来停止自动配置。然后使用DataSourceBuilder.create()手动构建以实现此目的。来源:https://www.baeldung.com/spring-boot-failed-to-configure-data-source - SydMK
做完这个之后,应该对我起作用了。显然,我的问题是应用程序.yaml文件中的缩进不正确,因此整个数据源块没有被识别。在正确对齐缩进后,@jarosik的解决方案开始起作用了。 - undefined

75

您的问题在于spring batch spring-boot-starter-batch 依赖于一个 spring-boot-starter-jdbc 的传递性Maven依赖。

Spring Batch是一个用于构建可靠和容错的企业批处理作业的框架。它支持许多功能,例如重新启动失败的批处理、记录批处理执行状态等等。为了实现这一点,Spring Batch 使用数据库模式来存储已注册作业的状态,自动配置已为您提供所需数据源的基本配置,而此配置需要关系型数据库配置。

要解决这个问题,您必须包含一些数据库驱动程序,如 mysqlh2 等,以配置 url

更新: 只需像下面这样配置您的 application.yml 即可开始使用:

spring:
  datasource:
    driver-class-name: org.h2.Driver
    url: jdbc:h2:mem:localhost;DB_CLOSE_ON_EXIT=FALSE
    username: admin
    password:

当然,在您的pom.xml中包含h2驱动程序,如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
       ....
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
        </dependency>

....
    </dependencies>
...

</project>

这个动机是因为不能使用Mongo来管理Spring Batch的内部数据库,因为Mongo仅提供给项目读写器使用,而不是用于管理内部模式,也就是内部架构,而不是业务架构。查询是普通的SQL查询,内部抽象依赖于关系型数据库。需要具有ACID能力的数据库,因为每个批处理都会读取和写入一块工作,并保存该信息以便重新启动作业。NoSql解决方案不适合这种情况。

最终,您已配置关系数据库,以准备Spring Batch的内部功能,内部抽象不仅依赖于jdbc,也可以使用mongo,但只用于批处理的业务方面,通过项目读写器。

希望这能帮助您消除疑虑。


这是我的问题 - 如果使用MongoDB,我们仍然需要使用H2吗?我使用了H2,但问题仍然存在!! - Jeff Cook
我更新了答案。我尝试了一个简单的入门,并且在这个配置下它可以工作。我能够启动我的应用程序。 - Valerio Vaudi
12
您可以从Spring-Batch依赖中排除spring-jdbc,或者在应用程序类中排除数据源bean加载“@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})”。请注意保持原意并使内容通俗易懂。 - TecHunter
在mongo项目中安装h2是浪费时间且不合逻辑的。这个解决方案对我有用... @ValerioVaudi 感谢您提供的清晰度。 - Arun3x3

66

根本原因

JPA(Java持久性API)是ORM(对象关系映射)工具的Java规范。spring-boot-starter-data-jpa依赖项使得在spring boot框架的上下文中可以启用ORM。

Spring Boot应用程序的JPA自动配置功能尝试使用JPA数据源建立数据库连接。JPA DataSource bean需要数据库驱动程序才能连接到数据库。

数据库驱动程序应该作为Pom.xml文件的依赖项提供。对于外部数据库,如Oracle、SQL Server、MySql、DB2、Postgres、MongoDB等,需要数据库JDBC连接属性来建立连接。

您需要配置数据库驱动程序和JDBC连接属性以解决此异常:无法配置DataSource:未指定“url”属性,且无法配置嵌入式数据源。原因:无法确定合适的驱动程序类。

application.properties

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

应用程序配置文件(application.yaml)

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

通过编程

@SpringBootApplication(exclude =  {DataSourceAutoConfiguration.class })

我的问题是:在我的yml设置中,我把spring.autoconfigure.exclude放错了位置,比如说放到了spring.xxx.autoconfigure.exclude里面,将设置路径改正后解决了我的问题。 - Frank Wang
没有办法——它起作用了! - fnuff
有没有办法重新启用这个配置?我正在运行时创建一个数据库,然后我想要指定JDBC URL,以便JPA根据Java类执行任务并创建数据库表。 - Brayan Loayza

34

虽然不是问题的关键点(但可能与问题相关),但如果您正在启动一个新项目,想知道为什么会收到相同的错误提示,这可能来自依赖项部分中spring-boot-starter-data-jpaartifactId。我在下面提供了该依赖关系。您需要定义数据库以摆脱此问题。

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

8
在我的情况下,我注释了pom.xml中的spring-boot-starter-data-jdbc依赖项,问题就解决了!我想如果你已经准备好驱动程序和其他凭据,并且可以在application.properties中更新它们以使项目构建成功,那么只包含这些依赖项会很好。 - raikumardipak
我遇到了类似于这里描述的问题。我在玩弄JPA而不是直接使用Mongo,所以我通过Intellij添加了上面的依赖项。我决定我不喜欢JPA的方式,所以我将其从_pom.xml_中注释掉。那时我一定开始出现了问题,就像在顶部的问题中所指出的那样。我需要运行mvn clean install,然后从Intellij中重新加载所有Maven项目,以消除错误。 - Mike

30

排除DataSourceAutoConfiguration.class对我有用:

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class })

8
非数据库应用的最佳解决方案。 - Doogle
@SpringBootApplication(exclude = [DataSourceAutoConfiguration::class]):Kotlin。 - Sami

14

我曾遇到过同样的问题,通过添加<scope>provided</scope>来解决。

        <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-jpa</artifactId>
            <scope>provided</scope>
        </dependency>

Source: https://github.com/spring-projects/spring-boot/issues/13796#issuecomment-413313346


13

可能是因为在通过Spring Initializr创建项目时没有将您的资源目录添加到类路径中,因此您配置的应用程序永远不会加载application.properties文件。

要快速测试是否出现此问题,请将以下内容添加到您的application.properties文件中:

server.port=8081

现在当您运行应用程序时,您应该在Spring Boot控制台输出中看到类似于以下内容:

INFO  o.s.b.w.e.tomcat.TomcatWebServer - Tomcat started on port(s): **8081** (http) with context path ''

如果您的端口仍然是默认的8080而没有更改为8081,那么显然您的application.properties文件没有加载。

您还可以通过在命令行中运行gradle bootRun来检查应用程序是否运行。这很可能会奏效。

解决方案:

  1. 关闭IntelliJ,然后在项目文件夹内删除“.idea”文件夹
  2. 像以下一样重新导入您的项目到IntelliJ中:“Import Project” -> “选择只导入您的build.gradle文件”。 (IntelliJ将自动获取其余部分)
  3. 再次构建和运行应用程序

请参阅IntelliJ支持的官方答案: IDEA-221673


对我来说,只需右键单击资源文件夹,然后选择“标记目录为”->“资源根目录”即可。 - Balazs F.
我尝试了这个,Tomcat在8081上正确启动(所以文件被识别了),但是由于某种原因,排除DataSourceAutoConfig会使我的Spring Boot应用程序在初始化时“挂起”。我尝试过注释,但结果相同。就像之前没有错误一样,只是“冻结”:/ - msTam
这对我有效,我将端口从server.port=8081更改为server.port=8082 - Sergio Perez

12

这个链接很有帮助。

Spring Boot自动配置尝试根据添加到类路径中的依赖项自动配置bean。因为我们在类路径上有JPA依赖(spring-data-starter-jpa),它尝试对其进行配置。

问题:Spring Boot没有所有配置JPA数据源所需的信息,即JDBC连接属性。

解决方案:

  1. 提供JDBC连接属性(最佳实践)
  2. 通过排除某些AutoConfig类推迟提供连接属性(临时 - 应最终删除)

上述链接使用了DataSourceAutoConfiguration.class的排除操作。

@SpringBootApplication(exclude={DataSourceAutoConfiguration.class})

但这对我没有起作用。相反,我必须排除2个AutoConfig类:

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class, XADataSourceAutoConfiguration.class})

对我来说有效,但我仍然不明白为什么需要显式地排除它。 - Saurabh Verma
嘿 @SaurabhVerma,我已编辑答案以解释排除 AutoConfig 类的理由。 - bibangamba
仅排除DataSourceAutoConfiguration对于spring-boot-starter-parent2.2.1.RELEASE版本在我的情况下有效。 - user2918640
由于:java.lang.IllegalArgumentException:JPA元模型不得为空! - Satish Hawalppagol
如果您需要排除数据源,那么应用程序正在加载不必要的依赖项。我更愿意在pom中排除不需要的构件。https://maven.apache.org/guides/introduction/introduction-to-optional-and-excludes-dependencies.html#dependency-exclusions - Matus Babinsky

10
以下内容适用于2021年的spring-boot版本2.5.0
如果您的application.properties文件中至少包含以下条目:
spring.datasource.url=jdbc:mysql://localhost:3306/db
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

在您的pom.xml中的这些依赖项

<dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

你不应该有这个错误:
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
Reason: Failed to determine a suitable driver class

这在我的情况下是IDE的问题

无论您使用的是Eclipse还是IntelliJ,应用程序必须在真实环境中运行在Linux上。因此,为了验证是否是IDE的问题,请使用shell运行您的应用程序。

mvn spring-boot:run

如果它能够无错误地启动,那么问题就出在你的IDE上。
Eclipse
Eclipse IDE for Enterprise Java and Web Developers
Version: 2021-03 (4.19.0)
Build id: 20210312-0638

在我的情况下,我是通过右键单击spring boot项目中的经典Application.java来运行项目,然后选择“以Java应用程序方式运行”。
经过几个小时的研究,解决方案如下:
- 右键单击根spring boot项目 - 然后选择“以Java应用程序方式运行” - Eclipse会显示多个带有main方法的类 - 我选择了我的Application.java文件,然后点击运行
详细说明:
如果你查看spring boot源代码中的DataSourceProperties.determineDriverClassName方法,你会发现只需要driverClassName或driver-class-name和url这两个参数。如果没有提供,就会抛出异常。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接