1. Overview
1.概述
In this short tutorial, we’ll discuss what causes and what resolves the “Failed to configure a DataSource” error on a Spring Boot project.
在这个简短的教程中,我们将讨论什么原因以及如何解决Spring Boot项目中的“配置数据源失败 “错误。
We’ll resolve the issue using two different approaches:
我们将用两种不同的方法来解决这个问题。
- Defining the data source
- Disabling the auto-configuration of the data source
2. The Problem
2.问题
Suppose we have a Spring Boot project, and we’ve added the spring-data-starter-jpa dependency and a MySQL JDBC driver to our pom.xml:
假设我们有一个Spring Boot项目,并且我们已经将spring-data-starter-jpa依赖性和MySQL JDBC驱动程序添加到我们的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>
<scope>runtime</scope>
</dependency>
But when we run the application, it fails with this error:
但是,当我们运行该应用程序时,它却出现了这样的错误。
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
Let’s see why this is happening.
让我们看看为什么会发生这种情况。
3. The Cause
3.原因
By design, Spring Boot auto-configuration tries to configure the beans automatically based on the dependencies added to the classpath.
根据设计,Spring Boot自动配置试图根据添加到classpath的依赖关系自动配置Bean。
And since we have the JPA dependency on our classpath, Spring Boot tries to automatically configure a JPA DataSource. The problem is that we haven’t given Spring the information it needs to perform the auto-configuration.
由于我们的classpath上有JPA的依赖,Spring Boot试图自动配置JPA的DataSource。问题是,我们还没有给Spring提供执行自动配置所需的信息。
For example, we haven’t defined any JDBC connection properties, and we’ll need to do so when working with external databases such as MySQL and MSSQL. On the other hand, we won’t face this issue with in-memory databases such as H2 since they can create a data source without all this information.
例如,我们没有定义任何JDBC连接属性,在与外部数据库(如MySQL和MSSQL)合作时,我们需要这样做。另一方面,我们在使用H2等内存数据库时不会面临这个问题,因为它们可以在没有所有这些信息的情况下创建一个数据源。
4. Solutions
4.解决方案
4.1. Define the DataSource Using Properties
4.1.使用属性定义DataSource
Since the issue occurs due to the missing database connection, we can solve the problem simply by providing the data source properties.
由于问题的发生是由于缺少数据库连接,我们可以通过提供数据源属性来解决这个问题。
First, let’s define the data source properties in the application.properties file of our project:
首先,让我们在我们项目的application.properties文件中定义数据源属性。
spring.datasource.url=jdbc:mysql://localhost:3306/myDb
spring.datasource.username=user1
spring.datasource.password=pass
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
Or we can provide the data source properties in application.yml:
或者我们可以在application.yml提供数据源属性。
spring:
datasource:
driverClassName: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/myDb
username: user1
password: pass
4.2. Define the DataSource Programmatically
4.2.以编程方式定义DataSource
Alternatively, we can define our data source programmatically, by using the utility builder class DataSourceBuilder.
另外,我们可以通过使用实用的构建器类DataSourceBuilder,以编程方式定义我们的数据源。
We need to provide the database URL, username, password and the SQL driver information to create our data source:
我们需要提供数据库的URL、用户名、密码和SQL驱动信息来创建我们的数据源。
@Configuration
public class DatasourceConfig {
@Bean
public DataSource datasource() {
return DataSourceBuilder.create()
.driverClassName("com.mysql.cj.jdbc.Driver")
.url("jdbc:mysql://localhost:3306/myDb")
.username("user1")
.password("pass")
.build();
}
}
In short, we can choose to use any one of the above options to configure a data source as per our requirements.
简而言之,我们可以选择使用上述任何一个选项,按照我们的要求配置数据源。
4.3. Exclude DataSourceAutoConfiguration
4.3.排除DataSourceAutoConfiguration
In the previous section, we fixed the issue by adding the data source properties to our project.
在上一节中,我们通过向我们的项目添加数据源属性来解决这个问题。
But how do we solve this if we’re not yet ready to define our data source? Let’s see how to prevent Spring Boot from auto-configuring the data source.
但是,如果我们还没有准备好定义我们的数据源,我们该如何解决这个问题呢?让我们看看如何防止Spring Boot自动配置数据源。
The class DataSourceAutoConfiguration is the base class for configuring a data source using the spring.datasource.* properties.
DataSourceAutoConfiguration类是使用spring.datasource.*属性来配置数据源的基类。
Now, there are a few ways that we can exclude this from the auto-configuration.
现在,我们有几种方法可以将其排除在自动配置之外。
First, we can disable the auto-configuration using the spring.autoconfigure.exclude property in our application.properties file:
首先,我们可以使用spring.autoconfigure.exclude 属性在我们的application.properties文件中禁用自动配置功能。
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
And we can do the same using our application.yml file:
我们也可以用我们的application.yml文件做同样的事。
spring:
autoconfigure:
exclude:
- org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
Or we can use the exclude attribute on our @SpringBootApplication or @EnableAutoConfiguration annotation:
或者我们可以在我们的@SpringBootApplication或@EnableAutoConfiguration注释中使用exclude属性。
@SpringBootApplication(exclude={DataSourceAutoConfiguration.class})
In all the above examples, we disabled the auto-configuration of the DataSource. And this will not affect auto-configuring any other beans.
在上述所有的例子中,我们禁用了DataSource的自动配置。而这不会影响自动配置任何其他的Bean。
So, to sum up, we can use any one of the above methods to disable Spring Boot’s auto-configuration of the data source.
因此,总结起来,我们可以使用上述任何一种方法来禁用Spring Boot对数据源的自动配置。
Ideally, we should provide the data source information and use the exclude option only for testing.
理想情况下,我们应该提供数据源信息,只在测试时使用排除选项。
5. Conclusion
5.总结
In this article, we’ve seen what causes the “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.
接下来,我们讨论了如何在完全不配置数据源的情况下解决这个问题。
As always, the full code used in this article is available on GitHub.
一如既往,本文中使用的完整代码可在GitHub上获得。