解决 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/ 验证成功

相关推荐
IT_陈寒11 分钟前
Redis的DEL命令竟然没删掉数据?我踩的这个坑你得知道
前端·人工智能·后端
用户9385156350713 分钟前
Docker + Nginx + Node.js:从“我的电脑能跑,你的电脑跑不了”到一键部署
后端·nginx·docker
陆枫Larry15 分钟前
两步验证(2FA )到底是什么?
后端
JavaGuide19 分钟前
我用 Claude Code/ZCode+GLM-5.3 从零做了一款 Agent 游戏!
前端·后端
程序员清风31 分钟前
专业再升级!程序员专属显示器明基RD280UG上手实测!
java·后端·面试
刘立军1 小时前
RESTful 与契约优先:规范接口定义,统一接口设计范式
后端·架构·ai编程
JavaGuide2 小时前
GitHub 4.5 万+ Star!GitNexus 把代码仓库变成了 Claude Code / Codex 能查询的知识图谱
后端·ai编程
子兮曰2 小时前
DeepSeek Harness 架构深潜:一个把 Agent 运行时做成纯插件树的开源 Harness
前端·后端·deepseek
子兮曰3 小时前
AI Agent 完整入门指南:从 LLM 到生产落地的 30+ 个核心概念
前端·后端·agent
渔夫正在掘金4 小时前
Cordis 插件热插拔能力深度解析
后端·node.js