Spring Boot 2.5.8启动报错:DataSource配置失败求助
解决Spring Boot连接Docker PostgreSQL启动报错问题
问题描述
练习Spring Boot微服务时,尝试连接Docker上的PostgreSQL容器,启动项目时出现数据源配置失败错误,核心报错信息如下:
*************************** 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
完整启动日志:
:: Spring Boot :: (v2.5.8) 2023-05-19 19:03:52.670 INFO 20084 --- [ main] c.m.s.p.ProductServiceApplication : Starting ProductServiceApplication using Java 17.0.7 on goktug with PID 20084 (D:\product-service\target\classes started by GOKTUG in D:\product-service) 2023-05-19 19:03:52.673 INFO 20084 --- [ main] c.m.s.p.ProductServiceApplication : No active profile set, falling back to default profiles: default 2023-05-19 19:03:53.923 INFO 20084 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode. 2023-05-19 19:03:54.032 INFO 20084 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 90 ms. Found 1 JPA repository interfaces. 2023-05-19 19:03:55.109 INFO 20084 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http) 2023-05-19 19:03:55.121 INFO 20084 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2023-05-19 19:03:55.121 INFO 20084 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.56] 2023-05-19 19:03:55.259 INFO 20084 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2023-05-19 19:03:55.259 INFO 20084 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2507 ms 2023-05-19 19:03:55.345 WARN 20084 --- [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'dataSourceScriptDatabaseInitializer' defined in class path resource [org/springframework/boot/autoconfigure/sql/init/DataSourceInitializationConfiguration.class]: Unsatisfied dependency expressed through method 'dataSourceScriptDatabaseInitializer' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception; nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to determine a suitable driver class 2023-05-19 19:03:55.349 INFO 20084 --- [ main] o.apache.catalina.core.StandardService : Stopping service [Tomcat] 2023-05-19 19:03:55.368 INFO 20084 --- [ main] ConditionEvaluationReportLoggingListener : Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled. 2023-05-19 19:03:55.389 ERROR 20084 --- [ 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
错误原因分析
- 未激活配置剖面:日志显示
No active profile set,但数据库连接配置(url、username等)都放在了localhost配置剖面中,导致Spring Boot无法读取到数据源配置。 - YAML缩进错误:配置文件中存在多处缩进不规范的问题,比如
server节点嵌套在info下、spring.config.activate.on-profile缩进错误、jpa和datasource子节点缩进不一致,这些都会导致配置无法被正确解析。 - POM依赖冗余:存在重复的
lombok依赖,且javax.servlet-api依赖已被spring-boot-starter-web包含,可能引发依赖冲突。
解决方案
1. 激活localhost配置剖面
启动项目时添加参数指定激活localhost剖面:
- 命令行启动:
java -jar product-service-0.0.1-SNAPSHOT.jar --spring.profiles.active=localhost - IDE启动:在Run Configuration的Program Arguments中添加
--spring.profiles.active=localhost,或VM Options中添加-Dspring.profiles.active=localhost
也可以在配置文件默认剖面中直接设置激活:
spring: profiles: active: localhost
2. 修复YAML配置文件缩进问题
修改后的application.yml(统一使用2个空格缩进):
spring: application: name: product-service main: allow-bean-definition-overriding: true info: component: Product Service server: port: 9788 --- spring: config: activate: on-profile: localhost cloud: config: uri: http://localhost:8888 jpa: hibernate: ddl-auto: update database-platform: org.hibernate.dialect.PostgreSQLDialect properties: hibernate: default_schema: stock_management datasource: url: "jdbc:postgresql://localhost:5432/stock_management" username: admin password: admin
3. 清理POM中的冗余依赖
移除重复的lombok依赖和多余的javax.servlet-api依赖,修改后的pom.xml关键部分:
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-test</artifactId> <scope>test</scope> </dependency> <!--swagger--> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>3.0.0</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>3.0.0</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <scope>runtime</scope> </dependency> </dependencies>
额外检查项
- 确认Docker中PostgreSQL容器已启动,端口5432已映射到主机,
stock_management数据库已创建,admin用户拥有该库访问权限。 - 验证数据库连接URL格式正确,无多余引号或空格。
内容的提问来源于stack exchange,提问作者Goktug
相关产品推荐
相关产品推荐

