Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Spring Boot Error: Creating a Bean With 'DataSource' Defined in DataSourceAutoConfiguration

DZone 's Guide to

Spring Boot Error: Creating a Bean With 'DataSource' Defined in DataSourceAutoConfiguration

Learn more about the DataSourceAutoConfiguration error in Spring Boot.

· Java Zone ·
Free Resource

If you are using Spring Boot and getting errors like "Cannot determine embedded database driver class for database type NONE" or "Error creating a bean with name 'dataSource' defined in class path resource DataSourceAutoConfiguration," then you have come to the right place. In this article, we'll examine different scenarios in which this Spring Boot error comes up and what you can do to solve it. The general reason for this error is Spring Boot's auto-configuration, which is trying to automatically configure a DataSource for you but doesn't have enough information. It is automatically trying to create an instance of DataSourceAutoConfiguration bean and it's failing.

Like other Spring Frameworks errors, the stack trace looks quite messy, something that they could have improved with Spring Boot, but here are these two errors I mentioned above.

Let's see what the stack trace looks like in general:

@Configuration
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
public class SpringBootDemo {

  public static void main(String[] args) {
   SpringApplication.runSpringBootDemo.class, args);
  }

}


It is important to keep in mind that these errors can be intimidating if you are completely new to the Spring Framework. In that case, I suggest you go through a comprehensive Spring Boot course like Learn Spring Boot by Dan Vega on Udemy. Now, let's see some common causes of this error and what you can do to solve this problem.

1) Spring Boot Error Due to Starter Dependency

Some of my friends get this error even if they don't need a database. The main reason they were getting this error was because of a starter dependency. For example, some of them included spring-boot-starter-data-jpa, which then included hibernate-entitymanager.jar, and they didn't have additional things need to set that up.

Sometimes, including incorrect Starter POM, can also solve this problem like adding spring-boot-starter-jdbc  instead of spring-boot-starter-data-jpa dependency.

Spring Boot auto-configuration is triggered by JAR dependencies present in the classpath, and if it pulls something you don't need, then this type of error can occur.

Spring Boot Starter POM Dependency

2) Due to Missing Dependency

Sometimes, you do need a database but you forgot to include the driver JAR file into the classpath, which can also cause this error. For example, you have specified the following properties in the application.properties, Spring Boots configuration file but didn't include the corresponding MySQL JDBC driver into the classpath

@Configuration
@EnableAutoConfiguration(
exclude = {DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
public class SpringBootDemo {

  public static void main(String[] args) {
     SpringApplication.runSpringBootDemo.class, args);
  }

}


In order to solve this error, either you need to include the correct Starter POM dependency or you need to manually add the MySQL JDBC JAR file into the classpath. If you are interested, you can check out this tutorial to learn more about how to connect a Java application to a database using a MySQL database.

3) Due to Missing Configuration in Application.properties

Spring Boot is good at configuring in-memory databases like H2, HSQLDB, Derby, etc., and it can configure them just by adding their JAR files into the classpath, but for others, you need to give Spring Boot additional details like URL, DriverClass name, etc.

You can do that by adding some properties to the application.properties file with the spring.datasource prefix, as shown in the following example:

spring.datasource.url = jdbc:mysql://localhost/abc
spring.datasource.name=testme
spring.datasource.username=xxxx
spring.datasource.password=xxxx
spring.datasource.driver-class-name= com.mysql.jdbc.Driver spring.jpa.database=mysql
spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect


This will provide the Spring Boot auto-configuration component to configure the database for you. If you want to learn more about how auto-configuration works in Spring Boot, I suggest you go through a comprehensive Spring Boot course like Spring Boot: Efficient Development, Configuration, and Deployment on Pluralsight, which will also teach you the details behind @EnableAutoConfiguration   by writing your own auto configurations.

4) Exclude DataSourceAutoConfiguration

Sometimes, excluding DataSourceAutoConfigution can also solve this problem, especially if you don't need a database. This will prevent Spring Boot from automatically configuring the database and there won't be any errors. You can disable auto-configuration for certain classes by using the exclude attribute of the @EnableAutoConfiguration annotation of Spring Boot, as shown below:

org.springframework.beans.factory.BeanDefinitionStoreException: 
Factory method [public javax.sql.DataSource 
org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration$N
onEmbeddedConfiguration.dataSource()] threw exception; 
nested exception is org.springframework.beans.factory.BeanCreationException: 
Cannot determine embedded database driver class for database type NONE. 
  If you want an embedded database please put a supported one on the classpath.:

[INFO] org.springframework.beans.factory.BeanCreationException: 
Cannot determine embedded database driver class for database type NONE. 
If you want an embedded database please put a supported one on the classpath.



You can even exclude more than one classes using exclude attribute with @EnableAutoConfiguration as shown below:


spring.datasource.url = jdbc:mysql://localhost/test

spring.datasource.driver-class-name= com.mysql.jdbc.Driver


That's all for today onhow to solve the "Cannot determine embedded database driver class for database type NONE" or "Error creating a bean with name 'dataSource' defined in class path resource DataSourceAutoConfiguration" problem. In most cases, it is because auto-configuration doesn't have enough details required to configure the database, but sometimes, it's also the accidental trigger of database auto-configuration, which can be disabled using the exclude attribute of @EnableAutoConfiguration annotation.

Lastly, if you want to learn Spring Boot in depth, here are some useful resources for your learning:

Other Java and Spring Boot articles you may like:

  • 5 Spring Boot Features Every Java Developer Should Know (features)
  • Top 5 Free Courses to learn Spring and Spring Boot in 2019 (courses)
  • 5 Course to Master Spring Boot online in 2019 (courses)
  • 10 Things Java Developer should learn in 2019 (goals)
  • 10 Tools Java Developers use in their day-to-day life (tools)
  • 10 Tips to become a better Java developer in 2019 (tips)
  • 3 Best Practices Java Programmers can learn from Spring (best practices)
  • Top 5 Spring Boot Annotations Java Developers should know (annotations)
  • 5 books to learn Spring Boot and Spring Cloud (books)
  • 5 courses to learn Spring Boot in depth (courses)

Thanks for reading this article! If you liked my explanation and solution of this Spring Boot error, then please share with your friends and colleagues. 

Topics:
java ,bean ,spring ,spring boot ,error ,datasource ,datasourceautoconfiguration ,database ,mysql

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}