Spring Boot error message: Failed to configure a DataSource

Updated:

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

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

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

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.

@SpringBootApplication 
public class DemoApplication 
<dependency> 
  <groupId>org.springframework.boot</groupId> 
  <artifactId>spring-boot-starter-data-jpa</artifactId> 
</dependency> 

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.

Solution

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

@SpringBootApplication(exclude={DataSourceAutoConfiguration.class}) 

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

@SpringBootConfiguration 
@EnableAutoConfiguration 
@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:

spring.datasource.url=jdbc:postgresql://localhost:5432/your_database 
spring.datasource.username=your_username 
spring.datasource.password=your_password 

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 

WebApp built by Marco using SpringBoot 3.2.4 and Java 21, in a Server in Switzerland