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

相关推荐
Windeal17 分钟前
Agent ToolCall 循环怎么定制?PI Extension 与 DeepAgents Middleware 两条岔路深度对比
后端·openai
鱼人19 分钟前
targets 包实战:R 语言数据分析流水线自动化管理方案
后端
时雨__21 分钟前
一文搞懂 Python 并发:GIL、多线程/多进程/协程怎么选
后端
Anson43221 分钟前
Dubbo架构深度分析
后端
站大爷IP24 分钟前
global和nonlocal到底有什么区别?
后端
二月龙25 分钟前
从零开发 Shiny 交互式数据看板:本地运行到网页上线完整路径
后端
小强198830 分钟前
词云 + 情感分析:爬取评论数据做舆情可视化实战
后端
小强198830 分钟前
高颜值动态可视化:gganimate 制作时序动图与数据短视频
后端
鱼人30 分钟前
Shiny 模块化开发:大型数据分析平台拆分与代码复用实战
后端
长大198831 分钟前
R 语言空间地图实战:从城市热力图到地理分布图,一篇吃透
后端