zl程序教程

您现在的位置是:首页 >  工具

当前栏目

【异常】Failed to configure a DataSource: ‘url‘ attribute is not specified and no embedded datasource

Failed异常 to not and is No url
2023-09-14 09:04:55 时间

一、报错内容

***************************
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 (the profiles monitor are currently active).

二、报错原因

查看代码中的配置,看起来没有什么问题,但是就是启动时报错

@SpringBootApplication
public class SpringBootApplication {

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

因为是Dubbo微服务架构,集成了common包,里面POM文件中定义了MybatisPlus + MySQL相关的依赖,而SpringBoot扫描到了这些个依赖,但是我们又没有在配置文件中配置与Datasource相关的内容,因此会爆这个错。

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.4.3</version>
</dependency>

三、问题解决

直接在SpringBoot启动的时候,移除DataSource的扫描即可,
即将文件修改为@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})即可

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

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

    @Bean
    MeterRegistryCustomizer<MeterRegistry> configurer(@Value("${spring.application.name}") String applicationName) {
        return registry -> registry.config().commonTags("application", applicationName);
    }
}

https://blog.csdn.net/yetaodiao/article/details/126562162

四、扩展说明

4.1 一般网关不需要配置数据库

在这里插入图片描述
在这里插入图片描述