当前位置: 代码网 > it编程>编程语言>Java > SpringBoot引入Redis报org.springframework.data.redis.core.RedisTemplate类找不到错误问题

SpringBoot引入Redis报org.springframework.data.redis.core.RedisTemplate类找不到错误问题

2024年09月08日 Java 我要评论
springboot引入redis报org.springframework.data.redis.core.redistemplate在学习redis时,发现导入redistemplate和redis

springboot引入redis报org.springframework.data.redis.core.redistemplate

在学习redis时,发现导入redistemplate和rediscachemanager失败,反复思索,终于找到解决办法,至此记下以便日后查阅。

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);
        // 设置缓存的过期时间
        // 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>

类找不到问题解决

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com