一、Java 8+ Spring Boot 2.x 之前的自定义redis配置:
1. maven引入springboot集成的redis依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
2.application.properties配置文件中配置redis:
spring.data.redis.database=0 spring.data.redis.host=127.0.0.1 spring.data.redis.port=6379 #连接池最大连接数(-1 负值表示没有限制) spring.data.redis.jedis.pool.max-active=20 #连接池最大阻塞等待时间(-1 负值表示没有限制) spring.data.redis.jedis.pool.max-wait=-1 #连接池最大空闲连接 spring.data.redis.jedis.pool.max-idle=10 #连接池最小空闲连接 spring.data.redis.jedis.pool.min-idle=0 #连接超时时间 spring.data.redis.timeout=2000
3.自定义redis序列化配置类(使存入redis的key-value是可见的)
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
@Configuration
public class RedisSerializableConfig {
@Bean
@SuppressWarnings("all")
public RedisTemplate<String,Object> redisTemplate(LettuceConnectionFactory lettuceConnectionFactory){
//默认的序列化策略是JDK的,所以在存储的时候键和值都会先被序列化后再存储
RedisTemplate<String,Object> template = new RedisTemplate<>();
template.setConnectionFactory(lettuceConnectionFactory);
//1.修改键的序列化策略
template.setKeySerializer(new StringRedisSerializer());
//2.修改hash类型的value中key的序列化方式
template.setHashKeySerializer(new StringRedisSerializer());
//4.设置value的序列化策略
Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
template.setValueSerializer(jackson2JsonRedisSerializer);
//5.修改hash类型value中value的序列化方式
template.setHashValueSerializer(jackson2JsonRedisSerializer);
//让设置生效
template.afterPropertiesSet();
return template;
}
@Bean
public RedisSerializer<Object> redisSerializer() {
//创建JSON序列化器
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
//必须设置,否则无法将JSON转化为对象,会转化成Map类型
objectMapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL);
return new GenericJackson2JsonRedisSerializer(objectMapper);
}
}
这样配置好后,使用:
@Resource private RedisTemplate<String, Object> redisTemplate; public void test(){ redisTemplate.opsForValue().set("testKey", "testValue", 60 * 10); }
这样存入redis的值是明文可见的。
二、Java 17 + Spring Boot 3.x 自定义redis配置:
SpringBoot 3改变了自动注入redis连接会话对象,当手动配置redis序列化时,它不会自动注入LettuceConnectionFactory这个对象,SpringBoot推荐使用LettuceConnectionFactory连接池,它的好处不用说了。
为了解决找不到bean的问题,只需要在之前的自定义redis序列化配置类中手动注入LettuceConnectionFactory这个Bean,
// 创建 LettuceConnectionFactory,Spring Boot 自动配置 @Bean public LettuceConnectionFactory lettuceConnectionFactory() { return new LettuceConnectionFactory(); // 使用默认构造方法,配置会自动读取 }
如果不需要自定义redis序列化配置,那么就不需要写配置类了,springboot会自动帮我们初始化一个redis的LettuceConnectionFactory连接池加载application.properties配置文件中redis的配置。