首页  >  文章  >  数据库  >  Redis SpringBoot类怎么配置

Redis SpringBoot类怎么配置

WBOY
WBOY转载
2023-06-02 15:37:111125浏览

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类怎么配置的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文转载于:yisu.com。如有侵权,请联系admin@php.cn删除