RedisTemplate 不能按类型装配注入

本文介绍在Spring Boot项目中使用RedisTemplate时遇到的注入失败问题及解决方案。当使用@Autowired注解注入RedisTemplate时,可能会出现NoSuchBeanDefinitionException异常。文章通过调整依赖注入方式,从@Autowired改为@Resource成功解决问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天做RedisTemplate的测试,在Spring boot 中自动注入RedisTemplate,测试报错。

@Autowired
private RedisTemplate<Serializable,Serializable> redisTemplate; 

报错:

Caused by: 
org.springframework.beans.factory.NoSuchBeanDefinitionException: 

No qualifying bean of type 'org.springframework.data.redis.core.RedisTemplate<java.io.Serializable, java.io.Serializable>' available: 

expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: 

{@org.springframework.beans.factory.annotation.Autowired(required=true)}

看spring boot文档发现有这么一句:

If you add a @Bean of your own of any of the auto-configured types it will replace the default
 (except in the case of RedisTemplate the exclusion is based on the bean nameredisTemplatenot its type). 

spring boot文档

将代码改成:

@Resource 
private RedisTemplate<Serializable,Serializable> redisTemplate; 

测试通过。

原文

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值