【问题标题】:Failed to auto-configure a DataSource: 'spring.datasource.url' is not specified无法自动配置数据源:未指定“spring.datasource.url”
【发布时间】:2018-09-03 15:16:51
【问题描述】:

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

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

在 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

【问题讨论】:

    标签: java spring mongodb spring-boot spring-data-jpa


    【解决方案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 依赖并运行。它应该可以正常工作。

    【讨论】:

    • 是的,那是我的问题。我已经删除了 JPA 依赖项,现在它工作正常。谢谢。
    • 请注意spring-boot-starter-batch 引入了对 jdbc 的依赖,它会激活同样的错误
    • 是的,这解决了我的问题。在我的例子中是org.postgresql:postgresqldata-jpa。只需排除 data-jpa 就可以了。
    • 如果我的 pom.xml 中有 data-jpamysql-connector-java 会产生错误吗?
    • 我隐藏了 mongo db 依赖,但还是有同样的问题。
    【解决方案2】:

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

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

    【讨论】:

    • 即使我没有在我的 pom.xml 中同时使用 jpa 和 mongodb 依赖项,我也遇到了这个问题......所以我得到了它,因为 spring-boot-starter-batch 依赖项。你能解释一下为什么这个设置有效吗?
    • 它成功了,但是从客户端发送请求时出现错误:blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource
    • @AdirD 这与手头的问题无关,您需要将服务器设置为接受 CORS 请求
    【解决方案3】:

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

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

    【讨论】:

      【解决方案4】:

      似乎缺少 MongoDB 驱动程序。在pom.xml 中包含以下依赖项:

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

      【讨论】:

      • 感谢您的回答。我的 pom.xml 文件中已经有了这个依赖项。仍然失败。
      • spring-boot-starter-data-mongodb 和 spring-batch-starter 对我来说也失败了
      【解决方案5】:

      我遇到这个错误仅仅是因为我在 application.properties 文件中拼错了 spring.datasource.url 值并且我使用的是 postgresql:

      问题是: jdbc:postgres://localhost:&lt;port-number&gt;/&lt;database-name&gt;

      固定为: jdbc:postgresql://localhost:&lt;port-number&gt;/&lt;database-name&gt;

      注意:区别在于postgrespostgresql,两者是不同的东西。

      进一步的原因和解决方法可以找到here

      【讨论】:

        【解决方案6】:

        您基于数据的依赖正在尝试查找它们各自的实体,其中一个尚未创建,根据数据获取依赖并再次运行应用程序。

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

        【讨论】:

          【解决方案7】:

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

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

          【讨论】:

            【解决方案8】:

            添加您的依赖项,例如 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>
            

            【讨论】:

              【解决方案9】:

              在 gradle build 中我很简单:

              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')`**
              

              它对我有用。

              【讨论】:

                【解决方案10】:

                我只是在 application.properties 中输入错误的 jdbc url 时遇到了这个问题。希望这可以帮助某人: 之前:

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

                之后:

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

                【讨论】:

                  【解决方案11】:

                  @Bhabadyuti Bal 给我们一个很好的答案,你可以在 gradle 中使用:

                  compile 'org.springframework.boot:spring-boot-starter-data-jpa' 
                  compile 'com.h2database:h2'
                  

                  在测试时间:

                  testCompile 'org.reactivecommons.utils:object-mapper:0.1.0'
                  testCompile 'com.h2database:h2'
                  

                  【讨论】:

                    【解决方案12】:

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

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

                    【讨论】:

                      猜你喜欢
                      • 2018-10-09
                      • 1970-01-01
                      • 2023-02-21
                      • 2022-07-21
                      • 2019-09-12
                      • 2018-12-15
                      • 2021-05-14
                      • 2019-03-25
                      • 2019-08-24
                      相关资源
                      最近更新 更多