This article is participating in the Java Theme Month – Java Debug Notes EventActive link

[正 确] Failed to configure a DataSource: ‘url’ attribute is not specified and no embedded DataSource could be configured. Error resolution

Symptom When the SpringBoot project is started, the following exception information is displayed:

[danube-chaos] [2020- 08 -07 14:36:30.759] [1596782172244_hugR] [MZhugR] [ERROR] [localhost-startStop-1] [org.springframework.boot.diagnostics.LoggingFailureAnalysisReporter:42] -- []

***************************
APPLICATION FAILED TO START
***************************

Description:

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
// Failed to configure data source: The URL property is not specified and the embedded data source cannot be configured.

Reason: Failed to determine a suitable driver class// Cause: the correct driver class cannot be specified explicitly (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). // Suggestion: // If you need to load embedded database (H2.HSQL or Derby// If there are database Settings that need to be loaded from the specified configuration file, this configuration file needs to be called (currently no active configuration file).Copy the code

Key information:

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

Copy the code

Analysis of the

My project here did not use database, but introduced mysql and Mybatis dependencies in some dependencies, which caused SpringBoot to start and automatically loaded some dependencies

I don’t have the information I need to configure the database

The solution

I used the following solution to solve the problem.

First of all, in the POM file, the dependency of mysql and Mybatis is excluded

<exclusion>
    <artifactId>mybatis-spring</artifactId>
    <groupId>org.mybatis</groupId>
</exclusion>                
<exclusion>
    <artifactId>mysql-connector-java</artifactId>
    <groupId>mysql</groupId>
</exclusion>
Copy the code

The SpringBootApplication annotation is then modified in the entry class of the SpringBoot program

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

    public static void main(String[] args) { SpringApplication.run(WebBootstrap.class, args); }}Copy the code

@ SpringBootApplication (exclude = {DataSourceAutoConfiguration. Class}) said to cancel the automatic configuration of data sources, Don’t load DataSourceAutoConfiguration class to Springboot container

Restart the project. A local problem is resolved

However, when it was released to the test environment, it did not take effect. Finally, it was found that the code in the test environment did not pull the latest code. This is a container problem, which will not be discussed here

Finally, the solution was solved by updating the code

Other Solutions

In practice, however, there may be a variety of reasons for this exception

  1. The configuration file is missing or the configuration is incorrectly written. Check the configuration data and add the data source configuration to resolve the problem

  2. Check whether there is a complete configuration in the directory where the configuration file was compiled (target)

  3. When using yML file configuration, it is customary to have one or more Spaces or a “Tab” in front of the configuration attribute. In YML file configuration, it is not allowed to have indentation in front of the attribute. The solution is not indentation. Change it to the following. Indent it out, use yML file configuration must pay attention to the format, otherwise the details will cause a big mistake

  4. If you are a Maven multi-module project, check the Edit Configurations -> Use classpath of Module in the upper right corner of your IDEA. This column should be the module containing the application entry’s xxxApplication. Java and application.yml, that is, to check if the entry class and configuration file match

Basically, these are the possibilities. If there are others, feel free to discuss them in the comments section