来一份redistemplate的配置类
时间: 2023-08-15 16:40:18 浏览: 97
好的,以下是一个简单的 RedisTemplate 配置类示例:
```java
@Configuration
public class RedisConfig {
@Bean
public RedisConnectionFactory redisConnectionFactory() {
LettuceConnectionFactory lettuceConnectionFactory = new LettuceConnectionFactory();
lettuceConnectionFactory.setHostName("localhost");
lettuceConnectionFactory.setPort(6379);
lettuceConnectionFactory.afterPropertiesSet();
return lettuceConnectionFactory;
}
@Bean
public RedisTemplate<String, Object> redisTemplate() {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory());
redisTemplate.setDefaultSerializer(new Jackson2JsonRedisSerializer<>(Object.class));
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(Object.class));
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
}
```
这个配置类使用 Lettuce 作为 Redis 的客户端,配置了 Redis 的连接信息,并且设置了 RedisTemplate 的序列化方式为 Jackson2JsonRedisSerializer。注意需要引入相应的依赖,比如 Lettuce 和 Jackson。
阅读全文