已添加正确依赖仍无法引用org.springframework.data.redis的问题
问题描述
我是IntelliJ/Kotlin新手,在.kts文件中配置了Spring Boot相关依赖(包含spring-boot-starter-data-redis等),但在Main.java中导入org.springframework.data.redis.core.RedisHash和org.springframework.data.redis.core.index.Indexed时,IDE无法识别redis相关包,显示“unresolved reference to redis”,请问哪里操作有误?
我的.kts配置代码:
plugins { id("java") id("io.spring.dependency-management") version "1.0.12.RELEASE" id("org.springframework.boot") version "2.7.0" } group = "org.example" version = "1.0-SNAPSHOT" repositories { google() mavenLocal() maven( "https://jitpack.io" ) maven("https://maven.aliyun.com/repository/google/") maven("https://maven.aliyun.com/repository/public/") maven("https://maven.aliyun.com/repository/spring/") maven("https://maven.aliyun.com/repository/gradle-plugin/") maven("https://maven.aliyun.com/repository/spring-plugin/") mavenCentral() maven("https://plugins.gradle.org/m2") } dependencies { //https://medium.com/@krishnan.srm/springboot-kotlin-and-redis-great-partners-9207fe79c329 implementation("org.springframework.data:spring-data-commons") implementation("org.springframework.boot:spring-boot-starter-actuator") implementation("org.springframework.boot:spring-boot-starter-data-redis") implementation("org.springframework.boot:spring-boot-starter-web") implementation("org.springframework.data:spring-data-redis:2.7.0") implementation("com.fasterxml.jackson.module:jackson-module-kotlin") implementation("org.jetbrains.kotlin:kotlin-reflect") implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8") //implementation("org.jetbrains.kotlin:kotlin-gradle-plugin") implementation("org.springframework.boot:spring-boot-starter-data-jpa") implementation("org.springframework.boot:spring-boot-starter-mustache") implementation("org.springframework.boot:spring-boot-starter-web") implementation("com.fasterxml.jackson.module:jackson-module-kotlin") }
解决方案
问题主要出在依赖重复、冗余依赖以及插件配置不全这几个点,按以下步骤修复:
清理重复和冗余依赖
- 删除重复的
spring-boot-starter-web和jackson-module-kotlin依赖项,每组只保留一个 spring-boot-starter-data-redis已经包含了spring-data-redis,且Spring Boot的依赖管理插件会自动控制版本,无需手动添加org.springframework.data:spring-data-redis:2.7.0,直接删除该行
- 删除重复的
补充Kotlin插件
作为Kotlin项目,需要添加Kotlin JVM插件,在plugins块中加入:id("org.jetbrains.kotlin.jvm") version "1.6.21"(版本号选择与Spring Boot 2.7.0兼容的1.6.x系列即可)
修改后的完整配置
plugins { id("java") id("org.jetbrains.kotlin.jvm") version "1.6.21" id("io.spring.dependency-management") version "1.0.12.RELEASE" id("org.springframework.boot") version "2.7.0" } group = "org.example" version = "1.0-SNAPSHOT" repositories { google() mavenLocal() maven( "https://jitpack.io" ) maven("https://maven.aliyun.com/repository/google/") maven("https://maven.aliyun.com/repository/public/") maven("https://maven.aliyun.com/repository/spring/") maven("https://maven.aliyun.com/repository/gradle-plugin/") maven("https://maven.aliyun.com/repository/spring-plugin/") mavenCentral() maven("https://plugins.gradle.org/m2") } dependencies { implementation("org.springframework.data:spring-data-commons") implementation("org.springframework.boot:spring-boot-starter-actuator") implementation("org.springframework.boot:spring-boot-starter-data-redis") implementation("org.springframework.boot:spring-boot-starter-web") implementation("com.fasterxml.jackson.module:jackson-module-kotlin") implementation("org.jetbrains.kotlin:kotlin-reflect") implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8") implementation("org.springframework.boot:spring-boot-starter-data-jpa") implementation("org.springframework.boot:spring-boot-starter-mustache") }
- 同步Gradle并刷新IDE
- 在IntelliJ右侧Gradle面板点击刷新按钮(🔄),或者执行命令:
./gradlew clean build --refresh-dependencies - 若仍有问题,尝试执行
File -> Invalidate Caches... -> Invalidate and Restart重置IDE缓存
- 在IntelliJ右侧Gradle面板点击刷新按钮(🔄),或者执行命令:
内容的提问来源于stack exchange,提问作者Markus
相关产品推荐
相关产品推荐

