前言

(继续熬夜肝笔记ing…)
知识补充—>为什么JAVA对象需要实现序列化?
@autowired和@resource注解的区别是什么?


一、默认选择的序列化器

可能有小伙伴要问了,为啥前面的代码(SpringBoot快速整合Redis)没有设置序列化
是因为我们使用了@AutoWired,IOC容器为我们选择了StringRedisTemplate类来注入
咱们一起来追踪追踪一下源码:
我们进入RedisTemplate类里面,发现这个类有被其他类继承到,
在这里插入图片描述
咱们可以点进去继续跟踪源码,可以发现:StringRedisTemplate.java这个类
在这里插入图片描述
RedisSerializer.java
在这里插入图片描述
StringRedisSerializer.java
在这里插入图片描述
StringRedisTemplate默认选择的StringRedisSerializer序列化器

如果我们把Value的类型改为Object呢?
在这里插入图片描述
注意:这里继续使用@AutoWired会报错,需要使用@Resource,两者区别建议自己查资料(这里说一下为什么会报错:@AutoWired找不到该类型<String,Object>的Bean因为根本没有。使用@Resource直接注入的是RedisTemplate)
在这里插入图片描述

运行testSet代码:
在这里插入图片描述
为什么会变成这样嘞?
咱们继续查看源码,前面我们说到将String改为Obejct,注入的是RedisTemplate这个类,咱们就从这个类里面一探究竟:
在这里插入图片描述
这里可以看到有一个默认的序列化器,咱们使用Ctrl+F快速定位它new对象的地方:
在这里插入图片描述
可以看到RedisTemplate选择了默认的序列化器:
JdkSerializationRedisSerializer

二、配置序列化器

了解了那么多,那么下面我们开始配置序列化器吧
新建一个RedisConfig类

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.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * @program: testredis
 * @description: 配置序列化器
 * @author: ErFeng_V
 * @create: 2021-04-26 01:05
 */

@Configuration
public class RedisConfig{
    @Bean
    public RedisTemplate<String, Object>redisTemplate(RedisConnectionFactory factory){
        RedisTemplate<String,Object>template=new RedisTemplate<>();
        template.setConnectionFactory(factory);
        //设置key的序列化器
        template.setKeySerializer(new StringRedisSerializer());
        //设置value的序列化器
        template.setValueSerializer(new Jackson2JsonRedisSerializer<>(Object.class));
        return template;
    }
}



具体到底有啥用嘞?举个例子:
先写一个bean类

import lombok.AllArgsConstructor;
import lombok.Data;
import java.io.Serializable;

/**
 * @program: testredis
 * @description: 学生类
 * @author: ErFeng_V
 * @create: 2021-04-26 01:21
 */
@Data
@AllArgsConstructor
public class Student implements Serializable {
    private String name;
    private String age;
    private String sex;
}

进行test测试:

import com.yc.testredis.bean.Student;
import org.junit.Before;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import javax.annotation.Resources;

@RunWith(SpringRunner.class)
@SpringBootTest
class TestredisApplicationTests {

    @Autowired
    private RedisTemplate<String,Object> redisTemplate;
    @Test
    void testSet(){
        Student student=new Student("张三","19","男");
        redisTemplate.opsForValue().set("student",student);
        System.out.println(redisTemplate.opsForValue().get("student"));
    }

    @Test
    void testDelete(){
        if(redisTemplate.delete("student"))
            System.out.println("删除成功");
        else
            System.out.println("不存在该键");
    }
}

去看看效果:
在这里插入图片描述
!!!!肝完了肝完了


Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐