Partner – Microsoft – NPI (cat= Spring Boot)
announcement - icon

Azure Spring Apps is a fully managed service from Microsoft (built in collaboration with VMware), focused on building and deploying Spring Boot applications on Azure Cloud without worrying about Kubernetes.

And, the Enterprise plan comes with some interesting features, such as commercial Spring runtime support, a 99.95% SLA and some deep discounts (up to 47%) when you are ready for production.

>> Learn more and deploy your first Spring Boot app to Azure.

You can also ask questions and leave feedback on the Azure Spring Apps GitHub page.

1. Overview

In this short tutorial, we’ll discuss what causes and what resolves the “Failed to configure a DataSource” error on a Spring Boot project.

We’ll resolve the issue using two different approaches:

  1. Defining the data source
  2. Disabling the auto-configuration of the data source

Further reading:

Configuring a DataSource Programmatically in Spring Boot

Learn how to configure a Spring Boot DataSource programmatically, thereby side-stepping Spring Boot's automatic DataSource configuration algorithm.

Configuring Separate Spring DataSource for Tests

A quick, practical tutorial on how to configure a separate data source for testing in a Spring application.

Spring Boot With H2 Database

Learn how to configure and how to use the H2 database with Spring Boot.

2. The Problem

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:


But when we run the application, it fails with this error:


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

By design, Spring Boot auto-configuration tries to configure the beans automatically based on the dependencies added to the classpath.

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.

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.

4. Solutions

4.1. Define the DataSource Using Properties

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 file of our project:


Or we can provide the data source properties in application.yml:

    driverClassName: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/myDb
    username: user1
    password: pass

4.2. Define the DataSource Programmatically

Alternatively, we can define our data source programmatically, by using the utility builder class DataSourceBuilder.

We need to provide the database URL, username, password and the SQL driver information to create our data source:

public class DataSourceConfig {
    public DataSource getDataSource() {
        return DataSourceBuilder.create()

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

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.

The class DataSourceAutoConfiguration is the base class for configuring a data source using the spring.datasource.* properties.

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 file:


And we can do the same using our application.yml file:

    - org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration

Or we can use the exclude attribute on our @SpringBootApplication or @EnableAutoConfiguration annotation:


In all the above examples, we disabled the auto-configuration of the DataSource. And this will not affect auto-configuring any other beans.

So, to sum up, we can use any one of the above methods to disable Spring Boot’s auto-configuration of the data source.

Ideally, we should provide the data source information and use the exclude option only for testing.

5. Conclusion

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.

Course – LSD (cat=Persistence)

Get started with Spring Data JPA through the reference Learn Spring Data JPA course:

res – Persistence (eBook) (cat=Persistence)
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.