spring 无法配置数据源:未指定“url”属性,无法配置嵌入的数据源
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/51221777/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured
提问by Jeff Cook
I am working on a Spring Boot Batchexample with MongoDB and I have already started the mongodserver.
我正在使用 MongoDB 处理Spring Boot Batch示例,并且我已经启动了mongod服务器。
When I launch my application, I am getting the error below.
当我启动我的应用程序时,我收到以下错误。
Any pointers for this issue?
这个问题的任何指示?
***************************
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
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>
I have started mongodwith the following output:
我从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)
采纳答案by jarosik
check your application.properties
检查您的 application.properties
changing
改变
spring.datasource.driverClassName=com.mysql.jdbc.Driver
to
到
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
worked for me. Full config:
对我来说有效。完整配置:
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
回答by Valerio Vaudi
Your problem is the dependency of spring batch spring-boot-starter-batchthat has a spring-boot-starter-jdbctransitive maven dependency.
您的问题是spring-boot-starter-batch具有可spring-boot-starter-jdbc传递 maven 依赖项的 Spring Batch依赖项。
Spring Batch is a framework for building reliable and fault tolerance enterprise batch jobs. It supports many features like restarting a failed batch, recording the status of the batch execution and so on. In order to achieve that Spring Batch uses a database schema to store the status of the registered jobs, the auto-configuration already provides you the basic configuration of the required data source and it is this configuration that requires the relational database configuration.
Spring Batch 是用于构建可靠且容错的企业批处理作业的框架。它支持许多功能,例如重新启动失败的批处理、记录批处理执行的状态等。为了实现 Spring Batch 使用数据库模式来存储已注册作业的状态,自动配置已经为您提供了所需数据源的基本配置,正是这种配置需要关系数据库配置。
To solve this you must include some database driver like mysql, h2, etc. to configure the url.
为了解决这个问题,你必须包括像一些数据库驱动程序mysql,h2等配置url。
Update: Just for getting start you can configure your application.yml like below:
更新:只是为了开始,您可以像下面这样配置您的 application.yml:
spring:
datasource:
driver-class-name: org.h2.Driver
url: jdbc:h2:mem:localhost;DB_CLOSE_ON_EXIT=FALSE
username: admin
password:
and of course in your pom.xmlinclude the h2 dirver like this:
当然,在你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>
The motivation, because you can not use mongo for this purpose, is that the usage of mongo is provided only for item readers and writers and not for managing the internal database of Spring Batch that is an internal schema, not a business schema. The query is plain SQL query and the internal abstraction relies on a relational database. It is necessary to have a database with ACID capability because every batch reads and writes a chunk of work and saves that information in order to restart the job. A NoSql solution is not suitable for this.
动机,因为您不能为此目的使用 mongo,因为 mongo 的用法仅提供给项目读取器和写入器,而不是用于管理 Spring Batch 的内部数据库,该数据库是内部模式,而不是业务模式。查询是纯 SQL 查询,内部抽象依赖于关系数据库。有一个具有 ACID 功能的数据库是必要的,因为每个批次都会读取和写入一个工作块并保存该信息以重新启动作业。NoSql 解决方案不适合于此。
At the end you have configured a relational database in order to prepare Spring Batch for internal capability, the internal abstraction does not rely on mongo only on jdbc. Then mongo can be used but for the business side of the batch via item reader/writer.
最后,您已经配置了一个关系数据库,以便为内部功能准备 Spring Batch,内部抽象不仅仅依赖于 jdbc 上的 mongo。然后可以使用 mongo 但通过项目读取器/写入器用于批处理的业务方面。
I hope that this can help you to clear your doubts.
我希望这可以帮助您消除疑虑。
回答by Chaklader Asfak Arefe
Not to the point of the question (can be related though), but, if you bootstrap a new project and wondering why do you get the same error, it may come from the artifactIdof spring-boot-starter-data-jpain the dependency section. I gave the dependency below. You will need to define the database to get rid of this.
不是问题的重点(虽然可以相关),但是,如果您引导一个新项目并想知道为什么会遇到相同的错误,它可能来自依赖项部分中的artifactIdof spring-boot-starter-data-jpa。我在下面给出了依赖项。您将需要定义数据库以摆脱这种情况。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
回答by Kumar Anand
Just add : @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class })works for me.
只需添加:@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class })对我有用。
I was getting same error I tried with @EnableAutoConfiguration(exclude=...) didn't work.
我遇到了同样的错误,我尝试使用 @EnableAutoConfiguration(exclude=...) 没有用。
回答by runya
Excluding the DataSourceAutoConfiguration.classworked for me:
排除DataSourceAutoConfiguration.class对我有用的:
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class })
回答by Roger Pc
“Failed to configure a DataSource” error. First, we fixed the issue by defining the data source. Next, we discussed how to work around the issue without configuring the data source at all.
“无法配置数据源”错误。首先,我们通过定义数据源解决了这个问题。接下来,我们讨论了如何在不配置数据源的情况下解决此问题。
https://www.baeldung.com/spring-boot-failed-to-configure-data-source
https://www.baeldung.com/spring-boot-failed-to-configure-data-source
回答by bibangamba
This linkhelped.
这个链接有帮助。
Spring Boot auto-configuration tries to configure beans automatically based on the dependencies added to the classpath. And because we have a JPA dependency (spring-data-starter-jpa) on our classpath, it tries to configure it.
Spring Boot 自动配置尝试根据添加到类路径中的依赖项自动配置 bean。并且因为我们的类路径上有一个 JPA 依赖项 (spring-data-starter-jpa),它会尝试配置它。
The problem: Spring boot doesn't have the all the info needed to configure the JPA data source i.e. the JDBC connection properties. Solutions:
问题:Spring Boot 没有配置 JPA 数据源所需的所有信息,即 JDBC 连接属性。解决方案:
- provide the JDBC connection properties (best)
- postpone supplying connection properties by excluding some AutoConfig classes (temporary - should be removed eventually)
- 提供 JDBC 连接属性(最好)
- 通过排除一些 AutoConfig 类来推迟提供连接属性(临时 - 最终应删除)
The above link excludes the DataSourceAutoConfiguration.classwith
上面的链接不包括DataSourceAutoConfiguration.classwith
@SpringBootApplication(exclude={DataSourceAutoConfiguration.class})
But this didn't work for me. I instead, had to exclude 2 AutoConfig classes:
但这对我不起作用。相反,我不得不排除 2 个 AutoConfig 类:
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class, XADataSourceAutoConfiguration.class})
回答by Vinod
For spring boot version 2.X.Xbelow configuration worked for me.
对于2.X.X以下配置的spring boot版本对我有用。
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
Old jdbc driver is deprecated. The new one is mentioned on above configuration. Please use the same and restart the project.
旧的 jdbc 驱动程序已弃用。上面的配置中提到了新的。请使用相同的并重新启动项目。
回答by Zakhar Telyatnikov
I had the same issue resolved by add <scope>provided</scope>
我通过添加解决了同样的问题 <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>
Source: https://github.com/spring-projects/spring-boot/issues/13796#issuecomment-413313346
来源:https: //github.com/spring-projects/spring-boot/issues/13796#issuecomment-413313346
回答by Eric Ampire
I have add this annotation on the main class of my spring boot application and everything work perfectly
我在 spring boot 应用程序的主类上添加了这个注释,一切正常
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class })


