Consider defining a bean of type 'org.springframework.data.redis.core.HashOperations' 解决办法


问题描述:

使用Spring Boot最新版Spring Boot-2.0.3整合Redis,在启动项目时,遇到的一个bug,异常信息如下:

Description:

Field hashOperations in com.iap.springboot.service.IRedisService required a bean of type 'org.springframework.data.redis.core.HashOperations' that could not be found.


Action:

Consider defining a bean of type 'org.springframework.data.redis.core.HashOperations' in your configuration.

Disconnected from the target VM, address: '127.0.0.1:60388', transport: 'socket'

Process finished with exit code 1

该bug导致项目一直无法正常启动,说在IRedisService接口中缺少 hashOperations 的Bean,并建议定义一个Bean 类型的 HashOperations,如下图:
这里写图片描述


解决办法:

根据错误信息提示,在Redis配置文件中添加 HashOperations Bean类型的组件,并在IRedisService接口中整一个HashOperations 的在字段就可以了,

在Redis文件中,注入Bean组件 HashOperations ,加入内容如下:

    @Bean
    public HashOperations<String, String, Object> hashOperations(RedisTemplate<String, Object> redisTemplate) {
        return redisTemplate.opsForHash();
    }

如下图:
这里写图片描述

在IRedisService接口中整一个字段,进行调用;
内容如下:

    @Resource
    private HashOperations<String, String, T> hashOperations;

注意:一定要使用@Resource注解,而不能使用@Autowired注解哟!!!
如下图:
这里写图片描述

配置添加完成后,项目就可以成功启动了,如下图:
这里写图片描述


好了,关于 Consider defining a bean of type ‘org.springframework.data.redis.core.HashOperations’ 解决办法 就写到这儿了,如果还有什么疑问或遇到什么问题欢迎扫码提问,也可以给我留言哦,我会一一详细的解答的。
歇后语:“ 共同学习,共同进步 ”,也希望大家多多关注CSND的IT社区。

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值