微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Redis SpringBoot类怎么配置

这篇文章主要介绍“Redis SpringBoot类怎么配置”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“Redis SpringBoot类怎么配置”文章能帮助大家解决问题。

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.Redistemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * @ClassName : RedisConfig
 * @Description : redis
 * @Author : MJoeBoyae
 * @Date: 2021-06-26 22:52
 */
@Configuration
public class RedisConfig {

    // springboot启动后,自定义容器内部的redistemplate对象,
    @Bean
    public <T> Redistemplate<String, T> redistemplate(RedisConnectionFactory redisConnectionFactory) {
        Redistemplate<String, T> redistemplate = new Redistemplate<>();
        redistemplate.setConnectionFactory(redisConnectionFactory);
		
        // 使用GenericJackson2JsonRedisSerializer序列化器,可以序列化和反序列化带泛型的数组
        GenericJackson2JsonRedisSerializer genericJackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer();
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();

        redistemplate.setKeySerializer(stringRedisSerializer);
        redistemplate.setValueSerializer(genericJackson2JsonRedisSerializer);
        redistemplate.setHashKeySerializer(stringRedisSerializer);
        redistemplate.setHashValueSerializer(genericJackson2JsonRedisSerializer);

        // 在设置好redistemplate属性后,使用afterPropertiesSet()方法使得设置生效
        redistemplate.afterPropertiesSet();
        return redistemplate;
    }

}

关于“Redis SpringBoot类怎么配置”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注编程之家行业资讯频道,小编每天都会为大家更新不同的知识点。

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

相关推荐