This tutorial will help you to solve the error Failed to auto-configure a DataSource: ‘spring.datasource.url’ is not specified in your Spring Boot applications.
In a nutshell, this error happens if you have configured in your pom.xml file the spring-boot-starter-data-jpa dependency and you haven’t defined any datasource url in your application.properties
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency>
That will cause the following 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 how to fix the issue.
Remove the JPA starter if not needed
Firstly, if you added by mistake the JPA starter in your pom.xml or if you changed your persistence storage from RDBMS to MongoDB (as an example), just remove the jpa starter to fix the issue:
<!-- Remove this <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-mongodb</artifactId> </dependency>
Include the database configuration in application.properties
The second option, if you are planning to use an external DB like MySQL, is to make sure your application.properties contains the complete configuration. Also make sure that your pom.xml file has the JDBC Driver for your DB:
spring.datasource.url=jdbc:mysql://localhost/demodb spring.datasource.username=admin spring.datasource.password=password spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect spring.jpa.show-sql=true spring.jpa.hibernate.ddl-auto=update
And there is the pom.xml file:
<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>
Use an embedded Database
Finally, it’s worth mentioning that when using an Embedded Database, you don’t need to specify connection settings in application.properties. As a matter of fact, Spring Boot will use some default values for it. Just make sure you have the dependency for JDBC in-memory databases in your pom.xml. Example for H2 Database:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
Found the article helpful? if so please follow us on Socials