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

相关推荐
爱勇宝7 小时前
第 1 章:别把“需求文档”当成真正的需求
前端·后端·程序员
IT_陈寒12 小时前
闭包陷阱让我加了两天班,JavaScript你真行
前端·人工智能·后端
易协同低代码13 小时前
通达OA核心类库TD类深度解析
后端
Gopher_HBo13 小时前
Go语言学习笔记(十八)Gin处理Session
后端
谭光志14 小时前
工具塞满上下文窗口怎么办?深度拆解 AI Agent Tool Search 按需加载实现原理
前端·后端·ai编程
她说..14 小时前
Java 默认值设置方式
java·开发语言·后端·springboot
foggyprojects14 小时前
从0开始,一句话启动AI DataAgent
后端·数据分析·ai编程
郡杰14 小时前
一些基础和问题解决
后端
陈随易14 小时前
前端项目部署只要30秒
前端·后端·程序员
YIAN14 小时前
从零手写文件读取 MCP 服务:一文吃透 Model Context Protocol 全链路通信原理
前端·后端·mcp