首页 新闻 会员 周边 捐助

redistemplate注入为nul

0
[待解决问题]

@Component
public class RedisUtils {

private static Integer CACHE_DATE = 10;	//redis 失效时间

@Autowired
private RedisTemplate<String, Object> redisTemplate;


public Object get(String key) {
    return key == null ? null : redisTemplate.opsForValue().get(key);
}

public void set(String key,Object v) {
    redisTemplate.opsForValue().set(key,v);
}

@Configuration
public class RedisConfig {

// 以下两种redisTemplate自由根据场景选择


@Bean
@SuppressWarnings("all")
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
    RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
    template.setConnectionFactory(factory);
    Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
    ObjectMapper om = new ObjectMapper();
    om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
    om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
    jackson2JsonRedisSerializer.setObjectMapper(om);
    StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();

    // key采用String的序列化方式
    template.setKeySerializer(stringRedisSerializer);
    // hash的key也采用String的序列化方式
    template.setHashKeySerializer(stringRedisSerializer);
    // value序列化方式采用jackson
    template.setValueSerializer(jackson2JsonRedisSerializer);
    // hash的value序列化方式采用jackson
    template.setHashValueSerializer(jackson2JsonRedisSerializer);
    template.afterPropertiesSet();


    return template;}
胡萝卜馅的主页 胡萝卜馅 | 菜鸟二级 | 园豆:204
提问于:2020-12-19 21:23
< >
分享
所有回答(1)
0

可以参考这个工具类:

springboot下整合redis使用redisTemplate模板

楠木大叔 | 园豆:2083 (老鸟四级) | 2020-12-21 17:41
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册