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

移除Spring Boot项目JPA后出现类找不到异常的求助

移除Spring Boot项目中的JPA后启动异常问题

我有一个Spring Boot项目,想要移除JPA,但完成操作后启动时出现以下异常:

java.lang.ClassNotFoundException: org.springframework.boot.autoconfigure.flyway.FlywayMigrationScriptMissingFailureAnalyzer
        at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641) ~[na:na]
        at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188) ~[na:na]
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:526) ~[na:na]
        at java.base/java.lang.Class.forName0(Native Method) ~[na:na]
        at java.base/java.lang.Class.forName(Class.java:534) ~[na:na]
        at java.base/java.lang.Class.forName(Class.java:513) ~[na:na]
        at org.springframework.util.ClassUtils.forName(ClassUtils.java:304) ~[spring-core-6.1.2.jar:6.1.2]
        at org.springframework.core.io.support.SpringFactoriesLoader.instantiateFactory(SpringFactoriesLoader.java:224) ~[spring-core-6.1.2.jar:6.1.2]
        at org.springframework.core.io.support.SpringFactoriesLoader.load(SpringFactoriesLoader.java:206) ~[spring-core-6.1.2.jar:6.1.2]
        at org.springframework.boot.diagnostics.FailureAnalyzers.loadFailureAnalyzers(FailureAnalyzers.java:69) ~[spring-boot-3.2.1.jar:3.2.1]
        at org.springframework.boot.diagnostics.FailureAnalyzers.<init>(FailureAnalyzers.java:64) ~[spring-boot-3.2.1.jar:3.2.1]
        at org.springframework.boot.diagnostics.FailureAnalyzers.<init>(FailureAnalyzers.java:58) ~[spring-boot-3.2.1.jar:3.2.1]
        at java.base/jdk.internal.reflect.DirectConstructorHandleAccessor.newInstance(DirectConstructorHandleAccessor.java:62) ~[na:na]
        at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:502) ~[na:na]
        at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:486) ~[na:na]
        at org.springframework.core.io.support.SpringFactoriesLoader$FactoryInstantiator.instantiate(SpringFactoriesLoader.java:382) ~[spring-core-6.1.2.jar:6.1.2]
        at org.springframework.core.io.support.SpringFactoriesLoader.instantiateFactory(SpringFactoriesLoader.java:228) ~[spring-core-6.1.2.jar:6.1.2]
        at org.springframework.core.io.support.SpringFactoriesLoader.load(SpringFactoriesLoader.java:206) ~[spring-core-6.1.2.jar:6.1.2]
        at org.springframework.core.io.support.SpringFactoriesLoader.load(SpringFactoriesLoader.java:160) ~[spring-core-6.1.2.jar:6.1.2]
        at org.springframework.boot.SpringApplication.getSpringFactoriesInstances(SpringApplication.java:491) ~[spring-boot-3.2.1.jar:3.2.1]
        at org.springframework.boot.SpringApplication.getExceptionReporters(SpringApplication.java:835) ~[spring-boot-3.2.1.jar:3.2.1]
        at org.springframework.boot.SpringApplication.handleRunFailure(SpringApplication.java:819) ~[spring-boot-3.2.1.jar:3.2.1]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:347) ~[spring-boot-3.2.1.jar:3.2.1]
        at com.cbusha.be.BackendApplication.main(BackendApplication.java:24) ~[main/:na]

2024-03-24T11:52:15.124-04:00 TRACE 9108 --- [cbusha-be] [           main] o.s.boot.diagnostics.FailureAnalyzers    : Unable to instantiate factory class [org.springframework.boot.autoconfigure.jdbc.HikariDriverConfigurationFailureAnalyzer] for factory type [org.springframework.boot.diagnostics.FailureAnalyzer]

java.lang.NoClassDefFoundError: org/springframework/jdbc/CannotGetJdbcConnectionException
        at java.base/java.lang.Class.getDeclaredConstructors0(Native Method) ~[na:na]
        at java.base/java.lang.Class.privateGetDeclaredConstructors(Class.java:3549) ~[na:na]
        at java.base/java.lang.Class.getConstructors(Class.java:2227) ~[na:na]
        at org.springframework.core.io.support.SpringFactoriesLoader$FactoryInstantiator.findConstructor(SpringFactoriesLoader.java:405) ~[spring-core-6.1.2.jar:6.1.2]
        at org.springframework.core.io.support.SpringFactoriesLoader$FactoryInstantiator.forClass(SpringFactoriesLoader.java:394) ~[spring-core-6.1.2.jar:6.1.2]
        at org.springframework.core.io.support.SpringFactoriesLoader.instantiateFactory(SpringFactoriesLoader.java:227) ~[spring-core-6.1.2.jar:6.1.2]
        at org.springframework.core.io.support.SpringFactoriesLoader.load(SpringFactoriesLoader.java:206) ~[spring-core-6.1.2.jar:6.1.2]
        at org.springframework.boot.diagnostics.FailureAnalyzers.loadFailureAnalyzers(FailureAnalyzers.java:69) ~[spring-boot-3.2.1.jar:3.2.1]
        at org.springframework.boot.diagnostics.FailureAnalyzers.<init>(FailureAnalyzers.java:64) ~[spring-boot-3.2.1.jar:3.2.1]
        at org.springframework.boot.diagnostics.FailureAnalyzers.<init>(FailureAnalyzers.java:58) ~[spring-boot-3.2.1.jar:3.2.1]
        at java.base/jdk.internal.reflect.DirectConstructorHandleAccessor.newInstance(DirectConstructorHandleAccessor.java:62) ~[na:na]
        at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:502) ~[na:na]
        at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:486) ~[na:na]
        at org.springframework.core.io.support.SpringFactoriesLoader$FactoryInstantiator.instantiate(SpringFactoriesLoader.java:382) ~[spring-core-6.1.2.jar:6.1.2]
        at org.springframework.core.io.support.SpringFactoriesLoader.instantiateFactory(SpringFactoriesLoader.java:228) ~[spring-core-6.1.2.jar:6.1.2]
        at org.springframework.core.io.support.SpringFactoriesLoader.load(SpringFactoriesLoader.java:206) ~[spring-core-6.1.2.jar:6.1.2]
        at org.springframework.core.io.support.SpringFactoriesLoader.load(SpringFactoriesLoader.java:160) ~[spring-core-6.1.2.jar:6.1.2]
        at org.springframework.boot.SpringApplication.getSpringFactoriesInstances(SpringApplication.java:491) ~[spring-boot-3.2.1.jar:3.2.1]
        at org.springframework.boot.SpringApplication.getExceptionReporters(SpringApplication.java:835) ~[spring-boot-3.2.1.jar:3.2.1]
        at org.springframework.boot.SpringApplication.handleRunFailure(SpringApplication.java:819) ~[spring-boot-3.2.1.jar:3.2.1]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:347) ~[spring-boot-3.2.1.jar:3.2.1]
        at com.cbusha.be.BackendApplication.main(BackendApplication.java:24) ~[main/:na]
Caused by: java.lang.ClassNotFoundException: org.springframework.jdbc.CannotGetJdbcConnectionException
        at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641) ~[na:na]
        at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188) ~[na:na]
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:526) ~[na:na]
        ... 22 common frames omitted

我的初始Gradle依赖配置如下:

dependencies {

    testImplementation 'org.springframework.boot:spring-boot-starter-test'

    implementation platform('software.amazon.awssdk:bom:2.+')
    implementation 'software.amazon.awssdk:s3'
    implementation 'software.amazon.awssdk:rds'

    implementation 'org.springframework.boot:spring-boot-starter-actuator'
    implementation 'org.springframework.boot:spring-boot-starter-data-rest'
    implementation 'org.springframework.boot:spring-boot-starter-security'
    implementation 'org.springframework.boot:spring-boot-starter-oauth2-client'
    implementation 'org.springframework.boot:spring-boot-starter-oauth2-resource-server'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter-data-cassandra'
    implementation 'org.springframework.boot:spring-boot-starter-webflux'
    implementation 'org.springframework.boot:spring-boot-autoconfigure'
    implementation 'org.springframework.kafka:spring-kafka'
    implementation 'jakarta.validation:jakarta.validation-api:3.0.2'
    implementation 'com.googlecode.libphonenumber:libphonenumber:8.12.39'

    compileOnly 'org.projectlombok:lombok'
    annotationProcessor 'org.projectlombok:lombok'
    testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
}

我尝试添加以下依赖:

//    TODO Why do I need these
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.flywaydb:flyway-core'
    implementation 'org.liquibase:liquibase-core'

并在application.yml中配置:

spring:
  flyaway:
    enabled: false

但仍出现相同错误。已确认项目中无类使用JPA注解,还尝试移除spring-boot-autoconfigure依赖、精简依赖至仅web和cassandra相关、添加spring-boot-starter-data-jdbc等操作,问题依旧。项目使用Java 21(OpenJDK)及io.spring.dependency-management插件,请问我遗漏了什么,该如何移除JPA并解决这些问题?


问题根源

这些异常是因为Spring Boot自动配置的FailureAnalyzer组件在尝试加载与JDBC/Flyway相关的分析器类,但对应的依赖已被移除,导致类找不到。另外配置文件存在拼写错误,且部分依赖可能间接引入了JPA/JDBC相关组件。

解决步骤

1. 排除冗余的自动配置类

在启动类上通过@SpringBootApplication的exclude属性,直接排除不需要的JDBC/JPA/Flyway自动配置类:

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration;
import org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;

@SpringBootApplication(exclude = {
    DataSourceAutoConfiguration.class,
    FlywayAutoConfiguration.class,
    HibernateJpaAutoConfiguration.class,
    JdbcTemplateAutoConfiguration.class,
    JpaRepositoriesAutoConfiguration.class
})
public class BackendApplication {
    public static void main(String[] args) {
        SpringApplication.run(BackendApplication.class, args);
    }
}

2. 修复配置文件拼写错误

将配置中的flyaway改为正确的flyway,同时禁用数据源自动配置:

spring:
  flyway:
    enabled: false
  datasource:
    enabled: false

3. 清理依赖

  • 移除临时添加的spring-boot-starter-data-jpa、flyway-core、liquibase-core依赖
  • 保留spring-boot-autoconfigure(它是Spring Boot自动配置的核心,不能随意移除)
  • 检查spring-boot-starter-data-rest是否间接引入JPA,若不需要则排除:
implementation('org.springframework.boot:spring-boot-starter-data-rest') {
    exclude group: 'org.springframework.boot', module: 'spring-boot-starter-data-jpa'
}

4. 清理构建缓存

执行Gradle清理命令,避免旧缓存干扰:

./gradlew clean build

5. 检查Spring Factories文件

确认项目中没有自定义的META-INF/spring.factories文件包含JDBC/Flyway相关的FailureAnalyzer条目,若有则删除对应内容。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.27 06:24:54