How to Resolve “Failed to Configure a DataSource” Error:
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.
Cause:
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, 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 like MySQL and MSSQL. On the other hand, we won't face this issue with in-memory databases like H2 since they can create a data source without all this information.
So, If we are not using JPA or JDBC and still we are having this issue. We can do following things to get rid of this error:
The class DataSourceAutoConfiguration is the base class for configuring a data source using the spring.datasource.* properties.
First, we can disable the auto-configuration using the spring.autoconfigure.exclude property in our application.properties file:
application.properties
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
application.yaml
spring: autoconfigure: exclude: - org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
Or, we can use the exclude attribute on our @SpringBootApplication or @EnableAutoConfiguration annotation:
@SpringBootApplication(exclude={DataSourceAutoConfiguration.class})
No comments:
Post a Comment