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

相关推荐
Flittly2 小时前
【AgentScope Java新手村系列】(4)结构化输出
java·spring boot·spring·ai
techdashen2 小时前
Rust 基础设施团队 2025 Q4 回顾与 2026 Q1 计划
开发语言·后端·rust
神奇小汤圆2 小时前
互联网大厂精选面试八股文(附2026最新Java+AI高频题)| 建议收藏
后端
春天花会开1312 小时前
影像上传前置机网络架构设计模板(含VPN)
后端·架构
程序员cxuan2 小时前
Fable 5 的系统提示词被人扒出来了,精彩,太精彩了。
人工智能·后端·程序员
神奇小汤圆2 小时前
颠覆认知:Java 打破双亲委派 ≠ 彻底废弃双亲委派模型
后端
ZengLiangYi2 小时前
5 种 AI 对话数据格式全解析
后端·aigc·ai编程
ZengLiangYi2 小时前
本地向量数据库选型:vectra vs chroma vs hnswlib
javascript·数据库·后端
咖啡八杯3 小时前
【无标题】
java·后端·设计模式
mqiqe3 小时前
面试题-MyBatis 面试篇
java·面试·mybatis