一句话总结:Spring Boot 启动时试图自动配置数据库连接,但你在配置文件中既没提供数据库 URL,也没启用 H2/HSQLDB 等嵌入式数据库。


🚨 一、错误全貌(典型日志)


2026-04-12 12:04:26.318  INFO 21144 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2026-04-12 12:04:26.325  WARN 21144 --- [           main] o.a.c.loader.WebappClassLoaderBase       : The web application [ROOT] appears to have started a thread named [Abandoned connection cleanup thread] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
 java.lang.Object.wait(Native Method)
 java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:143)
 com.mysql.jdbc.AbandonedConnectionCleanupThread.run(AbandonedConnectionCleanupThread.java:43)
2026-04-12 12:04:26.333  INFO 21144 --- [           main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2026-04-12 12:04:26.339 ERROR 21144 --- [           main] 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).

Disconnected from the target VM, address: '127.0.0.1:7870', transport: 'socket'

Process finished with exit code 1



🔍 二、为什么会发生?—— 核心机制解析

Spring Boot 的 DataSourceAutoConfiguration 自动装配机制在启动时会:

  1. 检查 spring.datasource.url 是否存在;
  2. 若不存在,尝试查找 classpath 下是否有 嵌入式数据库驱动(如 H2、HSQLDB);
  3. 若两者都无 → 抛出此异常。

💡 关键点:只要你引入了 spring-boot-starter-jdbcspring-boot-starter-data-jpa / mybatis-spring-boot-starter,就会触发此检查!


🛠️ 三、解决方案(按场景分类)

✅ 场景 1:你确实需要连接数据库(如 MySQL、PostgreSQL)

步骤 1:确保配置文件正确

src/main/resources/application.yml(或 .properties)中添加:

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/your_db?useSSL=false&serverTimezone=UTC
    username: root
    password: your_password
    driver-class-name: com.mysql.cj.jdbc.Driver

⚠️ 常见陷阱

  • YAML 缩进错误(必须是 2 空格)
  • url 写成 jdbc-url(旧版 MyBatis 需要,新版 Spring Boot 用 url
  • 未添加数据库驱动依赖(如 mysql-connector-java
步骤 2:检查依赖(Maven 示例)
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
</dependency>

✅ 场景 2:你不需要数据库(如纯 API 服务、工具类项目)

方案 A:排除数据源自动配置(推荐)

在主启动类上加注解:

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class YourApplication {
    public static void main(String[] args) {
        SpringApplication.run(YourApplication.class, args);
    }
}
方案 B:临时使用嵌入式数据库(仅测试用)

添加 H2 依赖(无需安装数据库):

<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <scope>runtime</scope>
</dependency>

✅ 此时即使不配 url,Spring Boot 也会自动创建内存数据库。


✅ 场景 3:多模块项目 / Profile 配置未生效

问题表现:
  • application-dev.yml 中有配置,但启动时仍报错
  • 原因:未激活对应 profile
解决:
  1. application.properties 中指定:
    spring.profiles.active=dev
    
  2. 或启动时加参数:
    java -jar app.jar --spring.profiles.active=dev
    

❗ 注意:bootstrap.yml 中设置 spring.profiles.active 在 Spring Boot 2.4+ 默认不生效!


🧪 四、快速自检清单

检查项 是/否
项目是否真的需要数据库?
application.ymlspring.datasource.url 是否存在且格式正确?
数据库驱动依赖是否已添加?
当前激活的 profile 是否包含数据库配置?
主启动类是否误排除了必要配置?

💡 五、最佳实践建议

  1. 明确项目依赖:若不用 DB,不要引入 mybatis / jpa starter;
  2. 配置分离:开发/测试/生产环境使用不同 profile;
  3. 日志调试:启动时加 --debug 查看自动配置报告;
  4. IDE 提示:在 IntelliJ IDEA 中,YAML 文件会高亮无效属性。

📌 结语

这个错误看似简单,却暴露了 Spring Boot “约定优于配置” 背后的隐式逻辑。理解自动装配机制,比记住解决方案更重要。下次再遇到类似问题,你就能一眼看穿本质!

🔗 延伸阅读Spring Boot 官方文档 - Data Sources

遇到具体配置问题?欢迎留言你的 pom.xml + application.yml 片段,我来帮你诊断!

Logo

AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。

更多推荐