【问题标题】:Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured配置数据源失败:未指定“url”属性,无法配置嵌入式数据源
【发布时间】:2018-12-15 17:37:51
【问题描述】:

我正在使用 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).

application.properties:

# 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)

【问题讨论】:

    标签: spring spring-boot spring-batch


    【解决方案1】:

    对于使用 Spring Boot 2 的任何人:

    默认数据源实现是Hikari now instead of TomcatJDBC

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

    如果您提供了上述属性,但仍然收到 OP 的错误:

    说明:

    未能配置数据源:未指定“url”属性并且 无法配置嵌入式数据源。

    原因:无法确定合适的驱动程序类

    添加 Hikari jdbc url 属性以使用您的数据源 url 属性。

    spring.datasource.hikari.jdbc-url = ${spring.datasource.url}
    

    也结帐this answer

    【讨论】:

      【解决方案2】:

      Inside pom.xml file always keep the updated spring framework version. 我创建了一个带有 Spring 框架版本 2.5.5 的项目,当时它运行良好。几个月后,我发现它无法正常工作。然后我放了最新版的spring框架。然后就可以了。

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

      【讨论】:

        【解决方案3】:

        spring-boot-starter-data-jpa 依赖包括 spring-boot-starter-jdbc 对我们来说是可传递的依赖。 现在我们只需要将我们的配置添加到 application.properties 文件中:

        【讨论】:

        【解决方案4】:

        所以,我遇到了类似的问题,这个链接很有帮助

        我的理解是项目的预设需要有一个“RDBMS数据库”和一个“In Memory Database

        关系型数据库

        • mysql
        • Postgres
        • 神谕
        • SQL 服务器

        在内存数据库中

        • H2 数据库
        • HSQL 数据库
        • 德比数据库

        所以,当我选择这个预设时,一切都很好

        【讨论】:

          【解决方案5】:

          这不是什么大问题,只要给 添加Application.properties: ``

          spring.datasource.name=/名称/

          【讨论】:

            【解决方案6】:

            根本原因

            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 连接属性来修复此异常 无法配置数据源:未指定“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 })
            

            【讨论】:

            • 这正是根本原因。
            【解决方案7】:

            作为 2021 年最新春季启动版本的总结 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 IDE for Enterprise Java and Web Developers
            Version: 2021-03 (4.19.0)
            Build id: 20210312-0638
            

            在我的例子中,我在 spring boot 项目中的经典 Application.java右键单击 运行该项目,然后 run as java application强>

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

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

            详细说明

            如果您检查确切的方法错误DataSourceProperties.determineDriverClassName,您将看到只需要 driverClassName 或 dirver-class-name 和 url。

            【讨论】:

              【解决方案8】:

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

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

              【讨论】:

              • 是的。当 Spring boot 批处理不需要数据源时,这可能是真正的解决方案,我们可以排除它们。
              【解决方案9】:

              我已在我的 Spring Boot 应用程序的主类中添加了此注释,并且一切正常

              @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class })
              

              【讨论】:

                【解决方案10】:

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

                application.properties

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

                【讨论】:

                  【解决方案11】:

                  这只是意味着您已经下载了具有数据库依赖关系的spring starter 代码而没有配置您的数据库,所以它不知道如何连接。对于 Spring boot 版本2.18,请按照以下步骤进行修复。

                  1. 为您下载的驱动程序创建一个数据库,即mysql/mongo等。

                  2. 在您的applications.properties 文件中添加数据库连接信息。如果您的数据库是mongo,则为mysql 提供示例,将其更改为mongo

                  spring.datasource.url=jdbc:mysql://localhost:3306/db_name_that_you_created
                  spring.datasource.username=your_db_username_here
                  spring.datasource.password=your_db_pass_here
                  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
                  
                  1. 重新启动将要运行的服务器。

                  【讨论】:

                    【解决方案12】:

                    检查您的 application.properties 文件。 可能的原因之一是

                    • 您可能被添加了“Spring Data”maven 插件,但您没有 提供datastore details in application.properties file.

                    【讨论】:

                      【解决方案13】:

                      如果您使用 YAML 进行配置,则可能是缩进问题。彻底检查 YAML 文件。

                      【讨论】:

                        【解决方案14】:

                        我在开始一个新项目时遇到同样的错误。使用命令行对我有用。

                        ./gradlew bootRun
                        

                        【讨论】:

                          【解决方案15】:

                          对我来说,资源文件夹在 Maven 更新/构建中被排除在外。我去 Build Path>Source 发现 src/main/resources 有“Excluded **”。我删除了该条目(单击排除**>删除>应用并关闭)。

                          然后它工作正常。

                          【讨论】:

                            【解决方案16】:

                            如果您在 pom.xml 中添加了“spring-boot-starter-data-jpa”依赖项,请在依赖项中添加相应的数据库,如 h2 等。

                            【讨论】:

                              【解决方案17】:

                              在主java文件中添加这个注解

                              @EnableAutoConfiguration(exclude=DataSourceAutoConfiguration.class)
                              

                              【讨论】:

                                【解决方案18】:

                                如果您的 pom.xml 中有 JPA 依赖项,则只需将其删除。这个解决方案对我有用。

                                【讨论】:

                                  【解决方案19】:

                                  如果你使用的是 Gradle,rebuild Gradle 可以解决这个问题。

                                  【讨论】:

                                  • 哇,这为我解决了。我总是忘记解决方案。
                                  【解决方案20】:

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

                                  我遇到了与@EnableAutoConfiguration(exclude=...) 相同的错误,但没有成功。

                                  【讨论】:

                                  • 当我知道我需要在某些时候使用 DataSource 但不需要立即使用时,它非常适合。
                                  • 这确实有效,但该错误的原因是将数据库依赖项添加到 pom.xml 并且没有添加用于连接数据库的变量。 // 如果要连接数据库,必须在 application.properties 中的适当位置添加属性
                                  • 是的,这就是我一直在寻找的解决方案,因为我的应用程序还没有任何数据挂钩(目前还没有)。
                                  • 感谢 Op,此答案应标记为“已接受”。
                                  【解决方案21】:

                                  我认为在导入模块时您已经导入了另一个包,请转到模块并删除所有这些。之后从项目的包中导入模块

                                  【讨论】:

                                    【解决方案22】:

                                    可能是因为你有 jpa 依赖和插件...

                                    不使用就注释掉(build.gradle 或 pom 文件)

                                    e。 g.

                                    // kotlin("plugin.jpa") version "1.3.61"
                                    
                                    // implementation("org.springframework.boot:spring-boot-starter-data-jpa")
                                    

                                    【讨论】:

                                      【解决方案23】:

                                      我在 pom.xml 中删除了对 mybatis 的过时依赖,以使我的运行。

                                      【讨论】:

                                        【解决方案24】:

                                        排除 DataSourceAutoConfiguration.class 为我工作:

                                        @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class })
                                        

                                        【讨论】:

                                        • 非基于数据库的应用程序的最佳解决方案。
                                        【解决方案25】:

                                        可能是你的 resources 目录在通过 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,如下所示:“导入项目”->“仅选择要导入的 build.gradle 文件”。 (IntelliJ 会自动抓取其余部分)
                                        3. 再次构建并运行您的应用程序

                                        查看 IntelliJ Support 的官方回答: IDEA-221673

                                        【讨论】:

                                        • 对我来说,右键单击资源文件夹就足够了,然后选择“将目录标记为”->“资源根目录”。
                                        • 我试过了,Tomcat 在 8081 上正确启动(因此文件被识别)但出于某种原因,排除 DataSourceAutoConfig 会使我的 Spring Boot 应用程序在初始化时“挂起”。我尝试了注释,结果相同。没有以前的错误,只是'冻结'..:/
                                        【解决方案26】:

                                        这个对我有用,适用于 MySQL: (应用程序属性)

                                        spring.datasource.url=jdbc:mysql://localhost:3306/db?useSSL=false&useUnicode=true&useJDBCCompliantTimezoneShift=true&
                                        useLegacyDatetimeCode=false&serverTimezone=UTC
                                        spring.datasource.username=root
                                        spring.datasource.password=admin
                                        spring.jpa.hibernate.ddl-auto=create
                                        spring.jpa.show-sql=true
                                        

                                        【讨论】:

                                          【解决方案27】:

                                          我有同样的问题并尝试了上面的所有建议,但没有成功。我正在为未来的读者发布我的答案。在它工作正常之前,但不知何故又出现了。我通过从 pom.xml 中删除一些不必要的插件和依赖项解决了这个问题

                                          1. 首先,我将默认打包类型改为jar(Spring Boot Initializer在打包时给出了pom

                                            &lt;packaging&gt;jar&lt;/packaging&gt;

                                          2. 我无意中添加了一些插件:

                                            <plugin> <artifactId>maven-war-plugin</artifactId> <configuration> <attachClasses>true</attachClasses> <webXml>target/web.xml</webXml> <webResources> <resource> <directory>src/main/webapp</directory> <filtering>true</filtering> </resource> </webResources> </configuration> </plugin>

                                          我希望我的回答对某人有所帮助。

                                          【讨论】:

                                            【解决方案28】:

                                            This link 提供帮助。

                                            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})
                                            

                                            【讨论】:

                                            • 为我工作,但我仍然不明白为什么我需要明确排除它
                                            • 嘿@SaurabhVerma,我编辑了答案以解释排除AutoConfig 类的理由。
                                            • 仅排除 DataSourceAutoConfiguration2.2.1.RELEASE of spring-boot-starter-parent 上为我工作。
                                            • 引起:java.lang.IllegalArgumentException:JPA 元模型不能为空!
                                            【解决方案29】:

                                            如果数据源在application.resources 中定义,请确保它位于src/main 的正下方,并将其添加到构建路径中。

                                            【讨论】:

                                              【解决方案30】:

                                              这是因为@valerio-vaudi 说的。

                                              你的问题是spring batch的依赖 具有 spring-boot-starter-jdbc 的 spring-boot-starter-batch 传递maven依赖。

                                              但您可以通过配置设置主数据源来解决它

                                               @Primary
                                               @Bean(name = "dataSource")
                                               @ConfigurationProperties(prefix = "spring.datasource")
                                               public DataSource getDataSource() {
                                                    return DataSourceBuilder.create().build();
                                               }
                                              
                                               @Bean
                                               public JdbcTemplate jdbcTemplate(DataSource dataSource) {
                                                    return new JdbcTemplate(dataSource);
                                               }
                                              

                                              【讨论】:

                                                猜你喜欢
                                                • 2019-09-12
                                                • 2023-02-21
                                                • 2022-07-21
                                                • 2020-05-31
                                                • 2021-05-14
                                                • 2019-03-25
                                                • 2019-08-24
                                                • 2021-03-18
                                                • 2022-12-25
                                                相关资源
                                                最近更新 更多