@Configuration
@EnableCaching // 开启注解
public class RedisConfiguration extends CachingConfigurerSupport {
/**
* retemplate 相干配置
*
* @param factory
* @return
*/
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
// 配置连贯工厂
redisTemplate.setConnectionFactory(factory);
// 应用 Jackson2JsonRedisSerializer 来序列化和反序列化 redis 的 value 值(默认应用 JDK 的序列化形式)Jackson2JsonRedisSerializer jacksonSeial = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper om = new ObjectMapper();
// 指定要序列化的域,field,get 和 set, 以及修饰符范畴,ANY 是都有包含 private 和 public
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
// 指定序列化输出的类型,类必须是非 final 润饰的,final 润饰的类,比方 String,Integer 等会跑出异样
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jacksonSeial.setObjectMapper(om);
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(jacksonSeial);
redisTemplate.setValueSerializer(jacksonSeial);
redisTemplate.afterPropertiesSet();
return redisTemplate;
}