Failed to configure a DataSource 到底是哪里配错了?拼错属性名、缺驱动、连不上、密码错报的是同一个错吗?排除自动配置为什么没生效?这篇在 Spring Boot 4.0.8 和 MySQL 上逐个复现。
[一] 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 换了包,旧类名被静默忽略。