解决 Spring Boot 启动报错:数据源配置引发的启动失败

启动项目时,控制台输出了如下错误信息:

复制代码
Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled.
2025-04-14 21:13:33.005 [main] ERROR o.s.b.d.LoggingFailureAnalysisReporter [] - 

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

原因:

Spring Boot 在启动时会自动尝试注入数据源并配置 JPA(Java Persistence API)。然而,在当前项目的配置环境下,并没有提供明确的数据源url属性,也无法自动配置嵌入式数据源,同时还无法确定合适的驱动类。这就导致了数据源配置失败,及导致了启动失败

解决

我们需要在@SpringBootApplication注解中排除数据源和 JPA 的自动注入。通过这种方式,Spring Boot 在启动时就不会再尝试进行默认的数据源和 JPA 配置,从而避免因配置缺失而导致的启动失败。经过这样的调整后,项目成功启动。

增加:exclude={DataSourceAutoConfiguration.class}

复制代码
@SpringBootApplication(exclude={DataSourceAutoConfiguration.class})
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

验证

通过访问http://127.0.0.1:8082/ 验证成功

相关推荐
用户8356290780515 分钟前
Python 调整 Excel 工作表显示设置
后端·python
ServBay41 分钟前
API Key 泄露如何在 10 分钟内止血?AI 密钥管理必备
后端·ai编程
代码丰1 小时前
大 JSON 放进 Redis 之后,如何用 Stream 解耦耗时任务?
redis·后端·系统架构
武子康2 小时前
手机加语音配件,断开连接后谁来保住这段话?
人工智能·后端·agent
captain3763 小时前
Spring MVC(4)-响应
java·spring boot·后端
武子康3 小时前
Cosmos Curator 总览:从一条样本的组成,选对数据处理入口
人工智能·后端·agent
北冥you鱼4 小时前
Go 语言 CRUD 实战:从接口设计到真实业务场景
开发语言·后端·golang
锋行天下4 小时前
Langgraph 实现类似Langchain记忆上下文过长裁剪
人工智能·后端
YDS8294 小时前
Small Spring IOC篇:作用域和FactoryBean
java·后端·spring
没有气的汽水4 小时前
一次内存缓慢泄漏的排查:每次都 new 一个 OpenAI 客户端,问题出在事件循环上
后端