Spring Data Jpa 无法在Spring应用程序中确定合适的驱动程序类错误

s2j5cfk0  于 2023-05-17  发布在  Spring
关注(0)|答案(1)|浏览(123)

我的Spring应用程序在IDE(IntelliJ IDEA)中运行得很好,但当我尝试运行JAR文件时会抛出错误。错误: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; 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
我的应用程序.属性部分,与数据库相关:

spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/test
spring.datasource.username=test
spring.datasource.password=test
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#spring.jpa.show-sql=true
#spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect

build.gradle文件:

plugins {
    id 'org.springframework.boot' version '2.7.5'
    id 'io.spring.dependency-management' version '1.0.15.RELEASE'
    id 'java'
}

group = 'org.zimovit'
version = '0.8.0'
sourceCompatibility = '11'

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
}

task myFatJar(type: Jar){
    manifest{
        attributes 'Main-Class': 'org.zimovit.SpringGusliBot.SpringGusliBotApplication'
    }
    baseName = 'gusli-bot-jar'
    duplicatesStrategy = DuplicatesStrategy.EXCLUDE
    from{configurations.runtimeClasspath.collect {it.isDirectory() ? it : zipTree(it)}}
    with jar
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.springframework.boot:spring-boot-starter-webflux'
    implementation group: 'org.telegram', name: 'telegrambots-spring-boot-starter', version: '5.7.1'
    compileOnly 'org.projectlombok:lombok'
    runtimeOnly 'com.mysql:mysql-connector-j'
    annotationProcessor 'org.projectlombok:lombok'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    testImplementation 'io.projectreactor:reactor-test'
}

tasks.named('test') {
    useJUnitPlatform()
}

我试着改变application.propreties文件(注解掉的行),作为一些相关文章的建议,但它根本不起作用。尝试了不同版本的Java -也不起作用。我还尝试使用@SpringBootApplication(exclude={DataSourceAutoConfiguration.class})注解排除DataSourceAutoConfiguration,这改变了错误,并且使用此注解,即使在IDE中也无法运行应用程序。我还尝试添加`实现组:'org. springframework',名称:'spring-jdbc',版本:build. gradle中的'5.3.24'依赖项。我想,也许是需要添加一些其他的依赖项,或者是在myFatJar任务中有一些错误,但是到目前为止我还没有找到修复它的方法。
我尝试了Shadow和Ktor插件来构建fatJar,但错误仍然存在。我仍然认为问题在于我构建fatJar的方式,因为该应用程序在IDE中工作。也许我错过了一些重要的东西。

ymdaylpp

ymdaylpp1#

我已经application.properties在主类中使用@PropertySource(“classpath:/application.properties”)注解明确定义了www.example.com文件位置,现在它可以工作了。

相关问题