无法配置DataSource:未指定'url'属性,也无法配置嵌入数据源

Jef*_*ook 48 spring spring-batch spring-boot

我正在使用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).
Run Code Online (Sandbox Code Playgroud)

application.properties:

# Mongo database URI. Cannot be set with host, port and credentials.
spring.data.mongodb.uri=mongodb://localhost/test 
Run Code Online (Sandbox Code Playgroud)

的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>
Run Code Online (Sandbox Code Playgroud)

我已经开始了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)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

Kum*_*and 108

只需添加:@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class }) 对我有用。

我遇到了同样的错误,我试过@EnableAutoConfiguration(exclude=...)没有用。

  • Kotlin 中的 `@SpringBootApplication(exclude = [DataSourceAutoConfiguration::class])` (5认同)
  • 当我知道我需要在某个时候使用数据源但并不立即需要它时,效果非常好。 (3认同)
  • 它确实有效,但该错误的原因是将数据库依赖项添加到 pom.xml 中,并且没有添加用于连接到数据库的变量。// 如果你想连接到数据库,你必须在application.properties中的适当位置添加属性 (3认同)

Val*_*udi 27

你的问题是spring-boot-starter-batch具有spring-boot-starter-jdbc传递maven依赖关系的spring批处理的依赖关系.

Spring Batch是构建可靠和容错企业批处理的框架.它支持许多功能,如重试故障批量重启,记录批处理执行的状态等.为了实现该春季批处理使用数据库模式来存储已注册作业的状态,自动配置已经为您提供了所需数据源的基本配置,并且此配置需要关系数据库配置.

为了解决这个问题,你已经包含了一些数据库驱动程序,如mysql,h2等,并配置了url.

更新 只是为了开始你可以配置你的application.yml如下:

spring:
  datasource:
    driver-class-name: org.h2.Driver
    url: jdbc:h2:mem:localhost;DB_CLOSE_ON_EXIT=FALSE
    username: admin
    password:
Run Code Online (Sandbox Code Playgroud)

当然在你的pom.xml中还包括h2 dirver

<?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>
Run Code Online (Sandbox Code Playgroud)

因为你不能为这个propouse使用mongo的动机是mongo的使用仅提供给项目读取/写入器而不是用于管理作为内部模式而不是业务模式的spring批处理的内部数据库.查询是纯SQL查询,内部抽象依赖于关系数据库.有一个具有ACID功能的数据库是必要的,因为每个批处理读写一大块工作并保存信息以便重新启动作业NoSql不适合这个.

最后,您已经配置了一个关系数据库,以便为内部功能准备好弹簧批处理,内部抽象不会仅在jdbc上继承mongo.然后可以使用mongo,但是通过项目读取器/写入器可以用于批处理的业务方面.

我希望这种反思可以帮助你消除疑虑.

  • 你可以从spring-batch依赖项中排除spring-jdbc,或者在应用程序类中排除数据源bean加载`@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class})` (9认同)

jar*_*sik 20

检查你的application.properties

改变

spring.datasource.driverClassName=com.mysql.jdbc.Driver
Run Code Online (Sandbox Code Playgroud)

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
Run Code Online (Sandbox Code Playgroud)

为我工作.完整配置:

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
Run Code Online (Sandbox Code Playgroud)

  • @GhostDede 请检查 src/main/resources/application.properties (10认同)
  • 如果此驱动程序: "com.mysql.jdbc.Driver" 已弃用,请考虑使用此驱动程序: "com.mysql.cj.jdbc.Driver" 。这对我有用! (5认同)
  • intelij ide 中的 application.properties 在哪里? (3认同)

小智 20

排除DataSourceAutoConfiguration.class对我有用的:

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class })
Run Code Online (Sandbox Code Playgroud)

  • 非基于数据库的应用程序的最佳解决方案。 (3认同)

za-*_*-ek 14

我通过添加解决了同样的问题<scope>provided</scope>

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

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


Are*_*efe 13

并不是问题的重点(尽管可以关联),但是,如果您引导一个新项目并想知道为什么会得到相同的错误,则可能来自于依赖性部分中的artifactIdof spring-boot-starter-data-jpa。我在下面给出了依赖性。您将需要定义数据库来摆脱这种情况。

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

  • 就我而言,我在 pom.xml 中注释了 spring-boot-starter-data-jdbc 依赖项,问题就消失了!我想,仅当您准备好驱动程序和其他凭据时,最好包含这些依赖项,然后可以在 application.properties 中更新这些依赖项,以便项目成功构建。 (5认同)

bib*_*mba 11

这个链接有帮助。

Spring Boot 自动配置尝试根据添加到类路径中的依赖项自动配置 bean。并且因为我们的类路径上有一个 JPA 依赖项 (spring-data-starter-jpa),它会尝试配置它。

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

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

上面的链接不包括DataSourceAutoConfiguration.classwith

@SpringBootApplication(exclude={DataSourceAutoConfiguration.class})
Run Code Online (Sandbox Code Playgroud)

但这对我不起作用。相反,我不得不排除 2 个 AutoConfig 类:

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class, XADataSourceAutoConfiguration.class})
Run Code Online (Sandbox Code Playgroud)


小智 11

根本原因

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 连接属性来修复此异常 Failed to configure a DataSource: 'url' attribute is not specified and no embedding datasource can be configured。原因:无法确定合适的驱动程序类。

应用程序属性

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

应用程序.yaml

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

通过编程

@SpringBootApplication(exclude =  {DataSourceAutoConfiguration.class })
Run Code Online (Sandbox Code Playgroud)


JRi*_*dsz 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
Run Code Online (Sandbox Code Playgroud)

以及 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>
Run Code Online (Sandbox Code Playgroud)

不应该出现这个错误:

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
Run Code Online (Sandbox Code Playgroud)

在我的例子中是 IDE

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

mvn spring-boot:run
Run Code Online (Sandbox Code Playgroud)

如果启动没有错误,则问题出在您的 IDE 中

Eclipse IDE for Enterprise Java and Web Developers
Version: 2021-03 (4.19.0)
Build id: 20210312-0638
Run Code Online (Sandbox Code Playgroud)

就我而言,我通过右键单击Spring Boot 项目内的经典Application.java来运行该项目,然后作为 java 应用程序运行

经过几个小时的研究,解决方案是:

  • 右键单击root spring boot 项目
  • 然后作为java应用程序运行
  • Eclipse 显示了几个带有主要方法的类。
  • 我选择了Application.java然后运行

长解释

如果您检查 spring boot 源代码DataSourceProperties.defineDriverClassName您会发现只需要 driverClassName 或 dirver-class-name 和 url 。如果不是,则抛出异常


小智 8

“无法配置数据源”错误。首先,我们通过定义数据源解决了这个问题。接下来,我们讨论了如何在不配置数据源的情况下解决此问题。

https://www.baeldung.com/spring-boot-failed-to-configure-data-source

  • 我宁愿在这里解释一下答案。 (3认同)

小智 6

我在 spring boot 应用程序的主类上添加了这个注释,一切正常

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class })
Run Code Online (Sandbox Code Playgroud)


小智 6

我在代码中遇到了同样的问题,在 Application.java 文件中添加此代码帮助我解决了问题 -

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


Fla*_*uff 5

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

如果是这种情况,要进行快速测试,请将以下内容添加到您的 application.properties 文件中:

server.port=8081
Run Code Online (Sandbox Code Playgroud)

现在,在运行您的应用程序时,您应该会在 spring boot 控制台中看到如下输出:

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

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

您还可以检查您的应用程序是否gradle bootRun从命令行运行。这很可能会起作用。

解决方案:

  1. 关闭 IntelliJ,然后在您的项目文件夹中删除“.idea”文件夹
  2. 将您的项目重新导入 IntelliJ,如下所示:“导入项目”->“仅选择要导入的 build.gradle 文件”。(IntelliJ 会自动抓取剩下的)
  3. 再次构建并运行您的应用程序

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


归档时间:

查看次数:

134931 次

最近记录:

5 年,9 月 前