[Java] 【Spring Boot】DataSourceProperties$DataSourceBeanCreationException

1 minute read

Occurrence event

When I tried to make a web application using SpringBoot for easy operation verification, it failed at build time. I got the following 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

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

It seems that the data source setting has failed?

How to respond

Apparently, it was because I mentioned MyBatis, which I thought I would use later. After deleting MyBatis definition from pom.xml, it started normally, but there seems to be other solutions as below.

1. Add the EnableAutoConfiguration annotation.

Add annotations to the [Project name Application.java] file.

sample.java


@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
@SpringBootApplication
public class MyAppSampleAppApplication {

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

2. Set the data source obediently

application.properties


spring.datasource.url=jdbc:postgresql://localhost:5432/postgres
spring.datasource.username=postgres
spring.datasource.password=postgres
spring.datasource.driverClassName=org.postgresql.Driver
  • It is necessary to drop the driver in advance.

Supplement: How to set up multiple data sources

As I was searching, I found a description when I wanted to use multiple databases, so take a note. How to specify two data sources