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

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

enter image description here

760797 次浏览

你的问题是spring批处理spring-boot-starter-batch的依赖关系,它有一个spring-boot-starter-jdbc传递maven依赖关系。

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

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

< p > 更新: 作为开始,您可以配置应用程序。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用于此目的,所以动机是mongo的使用仅提供给项读取器和写入器,而不是用于管理Spring Batch的内部数据库,后者是一个内部模式,而不是业务模式。查询是普通SQL查询,内部抽象依赖于关系数据库。有必要拥有一个具有ACID功能的数据库,因为每个批处理读取和写入一个工作块,并保存该信息以便重新启动作业。NoSql解决方案不适合这种情况。

最后,您已经配置了一个关系数据库,以便为Spring Batch的内部功能做好准备,内部抽象不依赖于mongo,只依赖于jdbc。然后mongo可以通过项目读取器/写入器用于批处理的业务方面。

我希望这能帮助你消除疑虑。

检查你的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

不是问题的重点(虽然可能相关),但是,如果你引导一个新项目,并想知道为什么你得到相同的错误,它可能来自依赖项中spring-boot-starter-data-jpaartifactId。我在下面给出了依赖关系。您将需要定义数据库来消除这种情况。

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

在pom文件中添加h2依赖项可以解决这些问题。 ...... com.h2database h2 ......

" Failed to configure a DataSource "错误。首先,我们通过定义数据源来解决这个问题。接下来,我们讨论了如何在不配置数据源的情况下解决这个问题。

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

对于春季引导版本2.X.X,下面的配置适合我。

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

旧的jdbc驱动程序已弃用。新的配置在上面提到了。 请使用相同的并重新启动项目。< / p >

我通过添加<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>
< p >来源: https://github.com/spring-projects/spring-boot/issues/13796#issuecomment-413313346 < / p >

这个链接帮助。

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

问题:Spring引导没有配置JPA数据源所需的所有信息,即JDBC连接属性。 解决方案:< / p >
  1. 提供JDBC连接属性(最好)
  2. 通过排除一些AutoConfig类来延迟提供连接属性(临时的-最终应该删除)

上面的链接不包括DataSourceAutoConfiguration.class with

@SpringBootApplication(exclude={DataSourceAutoConfiguration.class})

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

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

这仅仅意味着你下载了一个带有数据库依赖关系的启动器代码,而没有配置你的数据库,所以它不知道如何连接。对于Spring引导版本2.18,执行以下步骤来修复它。

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

  2. 在你的applications.properties文件中添加db连接信息。如果你的db是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. 重新启动它将运行的服务器。

只需添加:@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class })

我得到了相同的错误,我尝试@EnableAutoConfiguration(exclude=...)没有工作。

对于那些想知道在哪里添加@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class })的人,因为用户也已经问过了。你需要把它添加到主应用程序类,在src>main>java下。默认情况下,它被设置为@SpringBootApplication

这是因为@valerio-vaudi说。

你的问题是spring batch的依赖关系 具有spring-boot-start -jdbc的Spring-boot-starter-batch

但你可以解决它,设置主数据源与您的配置

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

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

我已经在spring引导应用程序的主类上添加了这个注释,一切都运行正常

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class })

我也有同样的问题,尝试了上面所有的建议,但都没有奏效。我把我的答案贴出来供将来的读者参考。之前它工作正常,但不知怎的它又出现了。我通过从pom.xml中删除一些不必要的插件和依赖来解决这个问题

  1. 首先,我将默认的打包类型更改为jar (Spring Boot Initializer在打包中给出砰的一声)

    <packaging>jar</packaging> < / p >

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

    < p > <代码> & lt; plugin> & lt; artifactId> maven-war-plugin< / artifactId> & lt; configuration> & lt; attachClasses> true< / attachClasses> & lt; webXml>目标/ web.xml< / webXml> & lt; webResources> & lt; resource> & lt; directory> src / main / webapp< / directory> & lt; filtering> true< / filtering> & lt; / resource> & lt; / webResources> & lt; / configuration> 代码& lt; / plugin> < / > < / p > < /李>

我希望我的回答能帮助到一些人。

这个对我有用,MySQL: (应用程序属性)< / p >

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

当通过Spring Initializr创建一个项目时,你的资源目录可能没有添加到类路径中。所以你的应用永远不会加载这个应用。已配置的属性文件。

如果是这种情况,请将以下内容添加到应用程序中进行快速测试。属性文件:

server.port=8081

现在,当运行你的应用程序时,你应该在spring引导控制台中看到类似这样的输出:

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

如果您的端口仍然是默认的8080,而没有更改为8081,则您的应用程序。属性文件显然没有加载。

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

解决方案:

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

参见IntelliJ Support的官方回答: 想法- 221673 < / p >

排除DataSourceAutoConfiguration.class对我有用:

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class })

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

这可能是因为您有jpa依赖关系和插件……

如果不使用(build.;)Gradle或pom文件)

e . g。

// kotlin("plugin.jpa") version "1.3.61"


// implementation("org.springframework.boot:spring-boot-starter-data-jpa")

我认为当导入模块时,你已经导入了另一个包,去模块并删除它们。之后,从项目包中导入模块

如果你正在使用Gradle,重新构建Gradle可以解决这个问题。

如果您的pom.xml中有JPA依赖项,那么只需删除它。这个解决方案对我很有效。

在主java文件中添加此注释

@EnableAutoConfiguration(exclude=DataSourceAutoConfiguration.class)

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

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

然后它工作得很好。

enter image description here

当我开始一个新项目时,我遇到了同样的错误。我用命令行就行。

./gradlew bootRun

根本原因

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

spring引导应用程序的JPA自动配置特性尝试使用JPA Datasource建立数据库连接。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 })

如果您使用YAML进行配置,那么可能会出现缩进问题。彻底检查YAML文件。

检查你的应用。属性文件。 其中一个可能的原因是

  • 你可能会被添加“;Spring Data"Maven插件,而你不是 提供datastore details in application.properties file.

您需要配置数据库驱动程序和JDBC连接属性来修复此异常。配置数据源失败:没有指定' url '属性,无法配置嵌入式数据源。原因:无法确定合适的驱动程序类别。

application.properties

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

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

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

作为对最新的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

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

在我的情况下,我在spring boot项目内部的经典Application.java上运行右击,然后运行作为Java应用程序运行

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

右击根弹簧引导项目上,然后作为Java应用程序运行。Eclipse向我展示了几个带有主方法的类。我选择Application.java,然后选择运行

长解释

如果你检查确切的方法错误DataSourceProperties.determineDriverClassName,你会看到只有driverClassName或driver -class-name和url是必需的。

这不是什么大问题,付出就是了 在Application.properties中添加: “< / p >

spring.datasource.name = / 的名字 /

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

我所理解的是,预设的项目需要有一个"RDBMS数据库"内存数据库"

RDBMS数据库

  • mysql
  • Postgres
  • 甲骨文
  • SQL server

内存数据库

  • H2数据库
  • HSQL数据库
  • Derby数据库

所以,当我选择这个预设,所有工作都很好__abc0

< p > 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>

对于使用Spring Boot 2的用户:

默认的数据源实现是Hikari现在取代了TomcatJDBC

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

如果你提供了上述属性,仍然得到OP的错误:

描述:

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

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

添加Hikari jdbc url属性来使用数据源url属性。

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

还签出这个答案

也检查spring配置文件,默认情况下它是'default'配置文件,如果你的应用程序属性有不同的配置文件,如test, prod等,那么你需要设置它。对于eclipse,设置环境变量为 名称= spring.profiles.default值=测试< / p >

当我在IDE / STS(弹簧工具套装)中运行项目时,一切都很好 但是当我做一个罐子时,这个被扔了

不必要的空格;“;在“application.yml"文件会导致这种情况。

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

而不是调整my "application.yml"文件< br > 我只是移动了所有的语句在“application.yml”;文件< br > “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
< p > (你可以在url的末尾添加参数) < br > (spring.datasource.url = jdbc: mysql: / / localhost: 3306 / studentdb ? allowPublicKeyRetrieval = true& useSSL = false) < / em >

步骤1)将下列依赖项放在pom.xml文件中。

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

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>

步骤2)将以下代码放入应用程序。属性文件。

spring.datasource.url=jdbc:mysql://localhost:3306/employee_directory?
useSSL=false&allowPublicKeyRetrieval=true
spring.datasource.username=springstudent
spring.datasource.password=springstudent

步骤3)仔细检查是否适用。属性文件放在src/main/resources文件夹中。

步骤4)步骤3是非常关键的,你可以花一整天的时间在代码中查找错误,但真正的问题是application.properties文件的位置

一个选项解决方案,如果已经测试了所有最后的答案

如果你有title之类的错误

验证的.properties连接字符串是正确的。

然后在.pom文件中添加关于maven插件的代码块

        ...
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.1.0</version>
</plugin>


...

和更新项目。对我有用!!

我希望这能帮助你。

这一步你在这个链接页面找到。

执行goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources失败

https://exerror.com/failed-to-execute-goal-org-apache-maven-pluginsmaven-resources-plugin3-2-0resources/

< p >这里: Maven clean install: Failed to execute goal org.apache.maven.plugins: Maven -resources-plugin:3.2.0:resources < / p >

这意味着你缺少“使用属性定义数据源”;

(记住更改sql datasource, username和pass)

spring.datasource.url=jdbc:mysql://localhost:3306/basic_springboot
spring.datasource.username=root
spring.datasource.password=manhcong
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

你可以参考这个

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