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

相关推荐
ServBay25 分钟前
Claude Fable 5.5 测试曝光,但它在悄悄给你打分
后端·ai编程·claude
AI源码44 分钟前
旅游推荐管理系统
spring boot
IT规划师1 小时前
一文讲透五种发布机制:滚动、蓝绿、金丝雀、红黑与 AB
后端·面试
一条Go3 小时前
两套权限目录,三重闸门:Alioth 的多租户 RBAC 为什么这么拧
后端·程序员·github
Tokenfree3 小时前
把多模态 OCR 接进业务系统:四千张运单,三个坑
后端
用户859313061353 小时前
我给 AI Agent 写了个下载器:293KB 的网页,不再吃掉 7 万 token
后端
phil63 小时前
沙箱核心功能系列 06:编排调度与资源管理
后端
小兔子3 小时前
if (system(cmd) != 0) 判不出命令失败:它返回的是等待状态,不是退出码
后端
艾迪的技术之路3 小时前
16k 条慢查询,128 张分表,我这么解决
后端·github
dpharness3 小时前
我踩过的四个记忆坑,从预算到换机
前端·后端