Docker化多模块Gradle项目中Spring Boot连PostgreSQL遇驱动类错误
多模块Gradle Spring Boot项目Docker容器启动时数据库驱动类无法识别
我正在开发一个包含shared模块和spring-module-1的多模块Gradle Spring Boot项目,使用PostgreSQL作为数据库,同时为数据库和Spring Boot应用配置了Docker容器。但启动Spring Boot应用容器时,遇到如下错误:
java_app | 20:23:04.141 [main] WARN org.springframework.context.annotation.AnnotationConfigApplicationContext -- Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaConfiguration': Unsatisfied dependency expressed through constructor 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
相关配置
Docker Compose配置
version: '3.9' services: # db name used to connect to the database via pgadmin java_db: container_name: java_db image: postgres:12 ports: - "5432:5432" environment: POSTGRES_USER: postgres POSTGRES_PASSWORD: postgres POSTGRES_DB: postgres volumes: - pgdata:/var/lib/postgresql/data spring-module-1: container_name: java_app environment: # environment variables to be used by the spring-module-1 container - DATABASE_URL=jdbc:postgresql://java_db:5432/postgres - DATABASE_USERNAME=postgres - DATABASE_PASSWORD=postgres depends_on: - java_db # spring-module-1 depends on java_db and will wait for it to be ready before starting build: context: . dockerfile: Dockerfile image: spring-module-1:latest # name of the spring-module-1 image to be used ports: - "8080:8080" # expose port 8080 of the container to port 8080 of the host machine volumes: pgdata: { }
spring-module-1的build.gradle配置
plugins { id 'java' id 'org.springframework.boot' version '3.1.3' } group = 'org.example' version = '1.0-SNAPSHOT' repositories { mavenCentral() } bootJar { enabled = false } jar { enabled = true manifest { attributes( 'Main-Class': 'com.example.springmodule1.SpringModule1Application' ) } from { configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) } } } dependencies { implementation project(':shared') implementation group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: '3.1.3' implementation group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa', version: '3.1.3' implementation group: 'org.postgresql', name: 'postgresql', version: '42.6.0' implementation group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.10.0' testImplementation('org.springframework.boot:spring-boot-starter-test:3.1.3') } test { useJUnitPlatform() } // create a single Jar with all dependencies tasks.register('fatJar', Jar) { manifest { attributes 'Main-Class': 'com.example.springmodule1.SpringModule1Application' } archiveBaseName = "${rootProject.name}" duplicatesStrategy = DuplicatesStrategy.EXCLUDE from { configurations.compileClasspath.collect { it.isDirectory() ? it : zipTree(it) } } with jar }
spring-module-1的application.yaml配置
spring: datasource: password: ${DATABASE_PASSWORD} url: ${DATABASE_URL} username: ${DATABASE_USERNAME} jpa: hibernate: ddl-auto: update properties: hibernate: dialect: org.hibernate.dialect.PostgreSQLDialect jdbc: lob: non_contextual_creation: true
环境信息
- Docker版本:24.0.6,build ed223bc
- Spring Boot版本:3.1.3
- PostgreSQL驱动版本:42.0.6
运行步骤
- 本地执行Gradle的
fatJar任务构建胖包; - 执行
docker compose build命令; - 在Docker Compose文件目录下执行
docker compose up spring-module-1。
已尝试的解决方法
- 将
application.yaml中的${DATABASE_PASSWORD}、${DATABASE_URL}、${DATABASE_USERNAME}替换为Docker Compose中定义的具体值; - 尝试使用PostgreSQL驱动的旧版本(42.5.1、42.4.3)。
期望实现Spring Boot应用和PostgreSQL数据库在Docker中成功运行。
内容的提问来源于stack exchange,提问作者KosenkoA
相关产品推荐
相关产品推荐

