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


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管理的類。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM