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

猜你喜欢

转载自blog.csdn.net/Hello_World_QWP/article/details/81205921