SpringBoot引入Redis報(bào)org.springframework.data.redis.core.RedisTemplate類找不到錯(cuò)誤問(wèn)題
SpringBoot引入Redis報(bào)org.springframework.data.redis.core.RedisTemplate
在學(xué)習(xí)Redis時(shí),發(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è)置緩存的過(guò)期時(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;
}
}
報(bào)錯(cuò)引用:

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

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

