Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasourc

Posted fanshuyao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasourc相关的知识,希望对你有一定的参考价值。

Failed to configure a DataSource: ‘url‘ attribute is not specified and no embedded datasource could be configured.

 

一、问题描述

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

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

 

原因就是springboot找到数据源的配置,但配置是有的,而且是正确的,为什么提示这个错误?

 

二、解决方案:

其实这个问题是包(package)路径的问题

在Springboot中,会自动扫描主启动类(@SpringBootApplication)包及所有子包

package com.datasource;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;

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

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

}

 

当你修改了主启动类的包名时,如将com.datasource修改成com.ds,但之前的子包没有同时修改,就会出现获取不到url的问题。因为主主启动类自动扫描com.ds及其所有子包,但此时是没有子包的。

一个简单的现象就是:当类被spring纳入管理时,类右上角是有一个“S”字母的,当你修改包名后,以前受spring管理的bean就失效,导致springboot识别不了。

 

解决方法:

方法一:

保证类路径一致(尽量不要手动修改主启动类,如果命名不合适需要更改,就要同时修改,类路径保持一致。)。

 

方法二:

在主启动类,使用@Import或@ComponentScan,引入包路径不一致但需要受到springboot管理的类。

 

以上是关于Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasourc的主要内容,如果未能解决你的问题,请参考以下文章

struts报错严重: Dispatcher initialization failed Unable to load configuration.

Failed to upgrade Oracle Cluster Registry configuration(root.sh)

安装Intel HAXM:"failed to configure driver unknown error. failed to open driver"

Failed to configure a DataSource 'url' attribute问题解决

springboot启动报错:Failed to configure a DataSource

Springboot Failed to configure a DataSource: ‘url‘ attribute is not specified and no embedded