新机器编译Spring Boot应用时DataSource配置失败求助
Spring Boot启动报错:无法配置DataSource,无法确定合适的驱动类
在新机器上编译启动Spring Boot应用时出现报错,该应用在原开发机器可正常运行。已复制所有应用文件及配置,但启动时提示无法配置DataSource,无法确定合适的驱动类。相关信息如下:
错误日志
WARN 66721 --- [ restartedMain] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Failed to initialize dependency 'dataSourceScriptDatabaseInitializer' of LoadTimeWeaverAware bean 'entityManagerFactory': 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: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception with message: Failed to determine a suitable driver class 2024-12-07T16:31:34.911-05:00 INFO 66721 --- [ restartedMain] o.apache.catalina.core.StandardService : Stopping service [Tomcat] 2024-12-07T16:31:34.916-05:00 INFO 66721 --- [ restartedMain] .s.b.a.l.ConditionEvaluationReportLogger : Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled. 2024-12-07T16:31:34.925-05:00 ERROR 66721 --- [ restartedMain] 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).
pom.xml配置
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.3.1</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>f1-dashboard</artifactId> <version>0.0.1-SNAPSHOT</version> <name>f1-dashboard</name> <description>Formula 1 Dashboard</description> <properties> <java.version>21</java.version> </properties> <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-web</artifactId> </dependency> <dependency> <groupId>org.javassist</groupId> <artifactId>javassist</artifactId> <version>3.23.1-GA</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <version>42.6.0</version> <!-- Ensure you have the correct version --> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
application.properties配置
spring.datasource.url=jdbc:postgresql://localhost:5432/f1dashboard spring.datasource.username=postgres spring.datasource.password=1234 spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect spring.jpa.hibernate.ddl-auto=update
解决方法
检查配置文件位置
- 确保
application.properties放在src/main/resources目录下,Spring Boot默认仅加载该路径下的配置文件,路径错误会导致无法读取数据库配置。
- 确保
优化PostgreSQL依赖
- 去掉pom.xml中postgresql依赖的版本号,让Spring Boot parent统一管理版本,避免版本兼容问题:
<dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <scope>runtime</scope> </dependency> - 执行
mvn clean install重新编译,确保依赖正确下载到本地仓库。
- 去掉pom.xml中postgresql依赖的版本号,让Spring Boot parent统一管理版本,避免版本兼容问题:
验证数据库状态
- 检查新机器上PostgreSQL服务是否启动,端口5432是否开放,
f1dashboard数据库是否存在。可通过以下命令测试连接:psql -U postgres -d f1dashboard -p 5432 - 先解决数据库连接问题,再启动应用。
- 检查新机器上PostgreSQL服务是否启动,端口5432是否开放,
检查配置文件正确性
- 确认
application.properties无乱码、配置项无拼写错误(如spring.datasource.url是否正确),用户名和密码与新机器数据库一致。
- 确认
手动指定驱动类
- 在
application.properties中添加驱动类配置,避免自动识别失败:spring.datasource.driver-class-name=org.postgresql.Driver
- 在
内容的提问来源于stack exchange,提问作者Shaughn
相关产品推荐
相关产品推荐

