bigywater.tech
Spring · 156

Failed to configure a DataSource:怎么来的、怎么修

Failed to configure a DataSource 到底是哪里配错了?拼错属性名、缺驱动、连不上、密码错报的是同一个错吗?排除自动配置为什么没生效?这篇在 Spring Boot 4.0.8 和 MySQL 上逐个复现。

4 种
没有 url、拼错属性名、加不加驱动、starter-data-jpa 这四种场景的启动报错完全相同,都是 Failed to configure a DataSource
静默忽略
spring.autoconfigure.exclude 里写 Boot 3 时代的 DataSourceAutoConfiguration 类名,在 4.0.8 上既不生效也不报错
Unable to determine Dialect
starter-data-jpa 连不上数据库时,启动失败的最内层异常并没有写连接被拒绝

没有 url

实测 · 本机执行输出
  [一] spring-boot-starter-jdbc,没有驱动、没有 url
    说明: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
    建议: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).
  [二] starter-jdbc + MySQL 驱动,没有 url
    说明: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
    建议: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.datasource.ulr
    说明: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
    建议: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).
  [十三] starter-data-jpa + MySQL 驱动,没有 url
    说明: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
    建议: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).
  [三] starter-jdbc + H2,没有 url
    启动成功;select 1 → 1;DataSource 实现类 HikariDataSource
  [六] 用环境变量 SPRING_DATASOURCE_URL 设 url
    启动成功;select 1 → 1;DataSource 实现类 HikariDataSource

四种场景同一个报错;拼错属性名没有提示。

缺驱动

实测 · 本机执行输出
  [四] 设了 url 但 classpath 上没有对应的驱动(只有 starter-jdbc)
    没有失败分析器说明;顶层异常:org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframewor;最内层原因: java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver

设了 url 但没有驱动:没有分析器说明,最内层是 ClassNotFoundException。

连不上

实测 · 本机执行输出
  [七] url 正确,但端口连不上(starter-jdbc,随后执行 select 1)
    启动成功;但第一次用连接时失败:CannotGetJdbcConnectionException → ConnectException: Connection refused (connect failed)
  [八] 同样连不上,但用 spring-boot-starter-data-jpa(Hibernate 启动时要连)
    没有失败分析器说明;顶层异常:org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/spri;最内层原因: org.hibernate.HibernateException: Unable to determine Dialect without JDBC metadata (please set 'jakarta.persistence.jdbc.url' for common cases or 'hibernate.dialect' when a custom Dialect implementa
  [九] 密码不对
    启动成功;但第一次用连接时失败:CannotGetJdbcConnectionException → SQLException: Access denied for user 'app'@'127.0.0.1' (using password: YES)
  [十] 库不存在(Unknown database)
    启动成功;但第一次用连接时失败:CannotGetJdbcConnectionException → SQLSyntaxErrorException: Unknown database 'nope'

starter-jdbc 第一次用连接才失败;starter-data-jpa 启动就失败。

排除

实测 · 本机执行输出
  [十一] 用旧的全限定名排除 DataSourceAutoConfiguration(org.springframework.boot.autoconfigure.jdbc 包)
    说明: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
    建议: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).
  [十二] 用 Boot 4 的全限定名排除(org.springframework.boot.jdbc.autoconfigure 包)
    启动成功;容器里没有 JdbcTemplate Bean

排除用的类名在 Boot 4 换了包,旧类名被静默忽略。

分步演示

DataSource
本步的对象

    本文用到的工具与边界

    • 本机 macOS 27 arm64 / OpenJDK 21.0.12(Homebrew) / Spring Boot 4.0.8(Spring Framework 7.0.9、Tomcat 11.0.24) / Spring Cloud 2025.1.3;用 javac 编译、java 直接运行,依赖由 Maven 从 Maven Central 解析;数字每次运行会有小幅抖动(Spring Boot 4.0.8;MySQL 是本机自己起的实例;每个场景是一个独立的最小应用,非 Web;日志只保留 FailureAnalyzer 的输出;连不上的场景把 Hikari 的 connection-timeout 设成 3000 ms)
    • 没有验证:PostgreSQL、Oracle 等其他数据库的报错文字;多数据源配置;连接池 HikariCP 以外的实现;spring.datasource.* 在 application.yml 里缩进错误的情况;spring.config.import 导入的配置里的 url;Docker Compose 与 Testcontainers 自动提供连接信息的场景;SSL 与时区参数错误
    京ICP备2026067502号-1京公网安备11010502063487号