Spring Boot error message: Failed to configure a DataSource

When you start your Spring Boot application you can incur in a configuration 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 

In my case happened when I removed H2 from my pom.xml. More often it could happen if you add spring-data in your pom.xml but you don't access any database.

The automatic suggestion of Spring Boot is

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). 

Spring Boot during the initialization was looking for a DataSource to initialize a database, but it couldn’t find any in the classpath:

ConfigServletWebServerApplicationContext :  
Exception encountered during context initialization - cancelling refresh attempt:  
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'dataSourceScriptDatabaseInitializer' defined in class path resource 

Origin of the problem

In my case (and maybe yours) the error was used because I was using the @SpringBootApplication annotation and I added a dependency to Spring Data in the pom.

public class DemoApplication 

The interface (@SpringBootApplication) uses another interface @EnableAutoConfiguration.

EnableAutoConfiguration according to the documentation is * attempting to guess and configure beans that you are likely to need.*

When Spring parses all your classes, their imports, their dependencies etc. it will try to guess the services that you need in you application and, more prone to error, it will try to configure them.

In most cases there is some implicit reference to a Database in the packages imported.

In my case a reference to the HikariDataSource is found in the classLoader :

datasource spring source code

… and the DataSourceAutoConfiguration is activated.


To avoid the issue you can exclude DataSourceAutoConfiguration from the autoconfiguration:


SpringBootApplication allows to pass as parameter filters that won't be called during the parsing of the code:

@ComponentScan(excludeFilters =  
  @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class), 
  @Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) 
public @interface SpringBootApplication { 

Here the code source of Spring Boot that shows how the framework tries to initialize the database after scanning the classLoader to find compatible DataSources.

DataSourceAutoConfiguration spring source code

Missing configuration

Another possible issue that could cause the error is the missing configuration of the database in your Spring Boot configuration,
check if you have something similar in your application.properties:


If the configuration is not found Spring Boot will fail to create the DataSource Bean used by the DatabaseIntializer.

Unsatisfied dependency expressed through method 'dataSourceScriptDatabaseInitializer' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource 

Super fast WebApp built by Marco using SpringBoot 3 and Java 17 hosted in Switzerland