问题描述:

使用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社区。

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐