gradle 无法在SpringBootKotlin中从应用程序.yml配置数据源

acruukt9  于 2023-02-04  发布在  Spring
关注(0)|答案(1)|浏览(215)

我正在使用yml文件来配置数据源。但在执行jar文件后,我得到了异常。如果我从IntelliJ运行项目,yml工作正常
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

但如果我通过Kotlin配置,它就能工作

@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()

    }
}
72qzrwbm

72qzrwbm1#

取出您的自定义jar似乎解决了这个问题。

//tasks.withType<Jar> {
//  manifest {
//      attributes["Main-Class"] = "url.shortener.UrlShortenerApplicationKt"
//  }
//  duplicatesStrategy = DuplicatesStrategy.EXCLUDE
//  from({
//      configurations.runtimeClasspath.get().filter { it.name.endsWith("jar") }.map { zipTree(it) }
//  })
//
//}

相关问题