You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SpringBoot Kotlin中application.yml配置数据源启动Jar包失败

解决Spring Boot Jar包启动时无法通过application.yml配置PostgreSQL数据源的问题

问题描述

  • 在IntelliJ中直接运行项目,通过application.yml配置PostgreSQL数据源一切正常
  • 执行以下命令构建并运行Jar包时出现异常:
    ./gradlew build -x check   
    java -jar build/libs/UrlShortener-0.0.1-SNAPSHOT-plain.jar
    
  • application.yml配置内容:
    spring:
      datasource:
        url: jdbc:postgresql://localhost:5332/dbName
        username: username
        password: password
        driverClassName: org.postgresql.Driver
    
  • 抛出的异常信息:
    05:13:50.339 [main] ERROR org.springframework.boot.SpringApplication - Application run failed
    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
    
  • 项目build.gradle.kts配置中自定义了Jar打包任务,改用Kotlin代码硬编码配置数据源时,Jar包启动正常:
    @Configuration
    class Config {
    
        @Bean
        fun getDataSource(): DataSource {
            val dataSourceBuilder = DataSourceBuilder.create()
            dataSourceBuilder.driverClassName("org.postgresql.Driver")
            dataSourceBuilder.url("jdbc:postgresql://localhost:5332/dbName")
            dataSourceBuilder.username("username")
            dataSourceBuilder.password("password")
            return dataSourceBuilder.build()
    
        }
    }
    

原因分析

问题根源在于自定义的Jar打包任务和运行的Jar包类型:

  1. 你运行的-plain.jar是Gradle默认生成的不包含依赖的瘦Jar,而非Spring Boot插件生成的包含所有依赖的可执行Fat Jar
  2. 自定义的tasks.withType<Jar>任务未遵循Spring Boot的Fat Jar规范,导致PostgreSQL驱动类无法被类加载器正确识别,同时Spring Boot的自动配置机制无法正常解析application.yml中的数据源配置

解决方案

方案1:使用Spring Boot官方插件生成可执行Jar(推荐)

  1. 删除build.gradle.kts中自定义的tasks.withType<Jar>块,保留Spring Boot插件的默认打包逻辑
  2. 执行构建命令生成可执行Fat Jar:
    ./gradlew bootJar -x check
    
  3. 运行生成的Jar包(注意无-plain后缀):
    java -jar build/libs/UrlShortener-0.0.1-SNAPSHOT.jar
    

方案2:调整自定义打包任务(不推荐,仅当必须自定义时使用)

若需保留自定义打包逻辑,需确保打包后的Jar包含所有依赖并符合Spring Boot类加载规范:

tasks.withType<Jar> {
    manifest {
        attributes["Main-Class"] = "url.shortener.UrlShortenerApplicationKt"
        // 添加Spring Boot类路径属性
        attributes["Class-Path"] = configurations.runtimeClasspath.get().joinToString(separator = " ") { it.name }
    }
    duplicatesStrategy = DuplicatesStrategy.EXCLUDE
    from(configurations.runtimeClasspath.get().map { if (it.isDirectory) it else zipTree(it) })
    // 排除签名文件避免冲突
    exclude("META-INF/*.SF", "META-INF/*.DSA", "META-INF/*.RSA")
}

运行时需确保所有依赖Jar在类路径范围内,或优先使用方案1的官方方式更可靠。

验证

修改配置后重新构建并运行Jar包,application.yml中的数据源配置将被正常加载,不再抛出驱动类找不到的异常。

内容的提问来源于stack exchange,提问作者iamanbansal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 15:50:31