Java.lang.NoClassDefFoundError: org/springframework/core/NativeDetector 错误信息:找不到类定义错误:org/springframework/core/NativeDetector。

7

我正在阅读这个教程,以巩固关于Spring的技能,但在使用JPARepository时遇到以下依赖问题:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userPersistence' defined in persistence.UserPersistence defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Cannot resolve reference to bean 'jpaMappingContext' while setting bean property 'mappingContext'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jpaMappingContext': Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: org/springframework/core/NativeDetector

UserPersistence类的定义如下:

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.querydsl.QuerydslPredicateExecutor;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;


@RepositoryRestResource()
public interface UserPersistence extends JpaRepository<UserDAO, Integer>, JpaSpecificationExecutor<UserDAO>, QuerydslPredicateExecutor<UserDAO> {}

我正在使用以下这个build.gradle文件:

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

group = 'org.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter'
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.springframework.boot:spring-boot-starter-data-rest'
    implementation group: 'org.springframework.data', name: 'spring-data-commons', version: '2.4.5'
    implementation group: 'com.querydsl', name: 'querydsl-jpa', version: '4.4.0'
    implementation group: 'javax.persistence', name: 'javax.persistence-api', version: '2.2'
    implementation group: 'javax.validation', name: 'validation-api', version: '2.0.1.Final'
    compileOnly 'org.projectlombok:lombok'
    runtimeOnly 'com.h2database:h2'
    annotationProcessor 'org.projectlombok:lombok'
    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }
}

test {
    useJUnitPlatform()
}

感谢对此问题有任何了解的人!


3
尝试将Spring Boot升级到2.4.3版本。 - glovemobile
3
异常看起来像是一个依赖问题,可能是由于以下这行代码造成的:implementation group: 'org.springframework.data', name: 'spring-data-commons', version: '2.4.5'我不确定 data-commons 2.4.5 是否与 Spring 版本 2.3.9 兼容。尝试使用 glovemobile 的方法。如果不起作用,应该检查类是否在以下位置:org/springframework/core/ - Forgotten Dreamer
2
将Spring Boot版本更新为2.4.4成功了,感谢您的帮助! - sigma1510
1
@glovemobile请将您的评论发布为答案,以便获得投票和接受。 - Jens Schauder
2个回答

6

版本 2.4.4 :) - sigma1510
你的答案是解决办法,可能是因为你有一些SpringFramework的依赖,例如org.springframework:spring-web。因此,如果你想使用更新版本的Spring Boot,你必须升级Spring-boot和SpringFramework两者。 - Grzegorz Jasiński

5

当我将Spring Core版本更改为5.3.8时,它对我起作用了。

我更新了我的pom.xml文件中的以下条目。

<dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>5.3.8</version>
        </dependency>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接