zoukankan      html  css  js  c++  java
  • 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管理的类。

  • 相关阅读:
    uva 10370
    uva 10107
    uva 10038
    uva 488
    伪代码格式
    公众号的秘密,知道一个biz就够了
    ToolTip 概述
    swt
    Java GUI图形界面开发工具
    Java多线程-两个小球
  • 原文地址:https://www.cnblogs.com/fanshuyao/p/12765633.html
Copyright © 2011-2022 走看看