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



我已经从 spring Initializr 的基本春季启动应用程序中使用了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

由于您在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依赖性并运行。它应该正常工作。

转到存在应用程序的资源文件夹。

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

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

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

我遇到了此错误,仅仅是因为我在 application.properties 文件中拼写了spring.datasource.url值,而我正在使用PostgreSQL:

问题是: jdbc:postgres://localhost:<port-number>/<database-name>

已修复到: jdbc:postgresql://localhost:<port-number>/<database-name>

注意:区别是postgres&amp; postgresql,这两个是两种不同的东西。

可以在此处找到进一步的原因和解决方案

似乎缺少mongoDB驱动程序。包括以下对pom.xml的依赖性:

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

基于数据的依赖性正在尝试找到其各自的实体,这些实体尚未创建,请根据数据评论依赖项并再次运行该应用程序。

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

当您将jpa依赖项放在弹簧启动配置文件中时,就会发生此错误。解决方案是:Spring-boot文档

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

如果任何人在面对此问题之后都在这里
当" project.jar"生成
尽管当我的项目穿着IDE/STS(弹簧工具西装(运行时,每件事都很好。
这是一个出路:

不必要的空间;&quot在"应用"中文件可能导致此。

server:
  port: 8085

spring:
 datasource:
  url: jdbc:mysql://localhost:3306/studentdb
  username: root
  password: root
  driver-class-name: com.mysql.cj.jdbc.Driver
 jpa:
   hibernate:
    ddl-auto: update
   show-sql: true
   database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
 application:
  name: STUDENT-SERVICE

而不是调整我的" application.yml"文件
我只是将所有语句移动到" application.yml"中。将文件归档到
&quot" application.properties"文件并格式化了" .properties"中所需的语句。

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/studentdb
spring.datasource.username=root
spring.datasource.password=root
spring.jpa.hibernate.ddl-auto = update
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format.sql=true
spring.application.name=student-service
server.port=8085

和VOILà

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

gradle build i Simply:

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

它对我有用。

@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'

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

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

我只是在application.properties中简单地误入了我的jdbc url时遇到了这个问题。希望这对某人有帮助:之前:

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

之后:

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

相关内容

最新更新