admin管理员组文章数量:1794759
Consider defining a bean of type 'org.springframework.data.redis.core.HashOperations' 的解决
在spring boot整合redis时,本以为springboot已经实现了对redis的完美结合。只需要一个@Autowired就可以使用RedisTemplate模板了,结果项目启动出现了下面的惊喜。。。
很明显让我们指明RedisTemplate在项目中的配置。果然,好记性不如烂笔头,突然想起来在之前的项目中已经使用过redis,其中就有这部分内容,但自己负责的不是这个模块而已,很是尴尬。。。
所以我找到RedisTemplate注册bean.
package com.dsx.redis.configuration; 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.GenericJackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; /*** * * @ClassName: RedisConfig * @Description: redis配置类 * @author 曰业而安 * @date 2020年3月23日 * */ @Configuration public class RedisConfig { @Bean public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) { RedisTemplate<String, Object> template = new RedisTemplate<String, Object>(); template.setConnectionFactory(factory); // key采用String的序列化方式 template.setKeySerializer(new StringRedisSerializer()); // hash的key也采用String的序列化方式 template.setHashKeySerializer(new StringRedisSerializer()); // value序列化方式采用jackson template.setValueSerializer(new GenericJackson2JsonRedisSerializer()); // hash的value序列化方式采用jackson template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer()); template.afterPropertiesSet(); return template; } }至此,RedisTemplate就可以使用了,还有切记在分布式中我们对redis的序列化要采用一致的方式呦。不然会出现bug。还有很多小伙伴使用的是StringRedisTemplate,它是RedisTemplate的子类。
StringRedisTemplate只能存储String类型的,即StringRedisTemplate<String,String>。用这一个模板是不需要将我们的对象实现序列化的,因为我们在存储对象之前,必须要将对象转Json才行,不然会存储不进去的。值得我们注意的是:两个template千万不要操作相同的Key。它们对同一个key的操作会相互影响的。
以上就是对此次bug的小结,如有不同见解,欢迎留言呦。。。
版权声明:本文标题:Consider defining a bean of type &#039;org.springframework.data.redis.core.HashOperations&#039; 的解决 内容由林淑君副主任自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.xiehuijuan.com/baike/1686550091a80763.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论