SpringBoot引入Redis報org.springframework.data.redis.core.RedisTemplate類找不到錯誤問題
SpringBoot引入Redis報org.springframework.data.redis.core.RedisTemplate
在學(xué)習Redis時,發(fā)現(xiàn)導(dǎo)入RedisTemplate和RedisCacheManager失敗,反復(fù)思索,終于找到解決辦法,至此記下以便日后查閱。
pom.xml引入如下:
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
</dependency>RedisConfig類代碼:
package com.neo.SpringBoot.config;
import java.lang.reflect.Method;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport {
public KeyGenerator keyGenerator() {
return new KeyGenerator() {
@Override
public Object generate(Object target, Method method, Object... params) {
StringBuilder sb = new StringBuilder();
sb.append(target.getClass().getName());
sb.append(method.getName());
for (Object object : params) {
sb.append(object.toString());
}
return sb.toString();
}
};
}
@SuppressWarnings("rawtypes")
@Bean
public CacheManager cacheManager(RedisTemplate redisTemplate) {
RedisCacheManager rcm = new RedisCacheManager(redisTemplate);
// 設(shè)置緩存的過期時間
// rcm.setDefaultExpiration(60);//秒
return rcm;
}
@Bean
public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {
StringRedisTemplate template = new StringRedisTemplate(factory);
Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(
Object.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
template.setValueSerializer(jackson2JsonRedisSerializer);
template.afterPropertiesSet();
return template;
}
}
報錯引用:

解決
在pom.xml中加入版本號即可
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.7.1.RELEASE</version>
</dependency>類找不到問題解決

總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
- 如何解決Could not transfer artifact org.springframework.boot問題
- 程序包org.springframework.boot不存在的問題解決
- java:無法訪問org.springframework.boot.SpringApplication問題
- 程序包org.springframework不存在的解決辦法
- org.springframework.web.client.ResourceAccessException資源訪問錯誤的解決方法
- Java報錯:Error:java:?程序包org.springframework.boot不存在解決辦法
- SpringFramework中的數(shù)據(jù)校驗方式
相關(guān)文章
spring中的注解@@Transactional失效的場景代碼演示
這篇文章主要介紹了spring中的注解@@Transactional失效的場景代碼演示,@Transactional注解是Spring框架提供的用于聲明事務(wù)的注解,作用于類和方法上,需要的朋友可以參考下2024-01-01
SpringBoot之集成Druid數(shù)據(jù)庫連接池方式
這篇文章主要介紹了SpringBoot之集成Druid數(shù)據(jù)庫連接池方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2026-06-06
MyBatis自定義TypeHandler實現(xiàn)字段加密解密
本文主要介紹了MyBatis自定義TypeHandler實現(xiàn)字段加密解密,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧2025-03-03
基于SpringBoot和Vue實現(xiàn)頭像上傳與回顯功能
在現(xiàn)代Web應(yīng)用中,用戶個性化體驗尤為重要,其中頭像上傳與回顯是一個常見的功能需求,本文將詳細介紹如何使用Spring Boot和Vue.js構(gòu)建一個前后端協(xié)同工作的頭像上傳系統(tǒng),并實現(xiàn)圖片的即時回顯,需要的朋友可以參考下2024-08-08
基于eclipse-temurin鏡像部署spring boot應(yīng)用的實現(xiàn)示例
本文提供了基于eclipse-temurin鏡像部署Spring Boot應(yīng)用的詳細實現(xiàn)示例,通過使用Docker鏡像,可以輕松地創(chuàng)建和管理Spring Boot應(yīng)用程序的容器化環(huán)境,感興趣的可以了解一下2023-08-08

