错误代码强行运行
@Autowired
RedisTemplate template;
@Test
void test(){
template.opsForValue().set("www","zzz");
System.out.println(template.opsForValue().get("www"));
}
报错为:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.why.Redis02SpringbootApplicationTests': Unsatisfied dependency expressed through field 'template': No qualifying bean of type 'org.springframework.data.redis.core.RedisTemplate<?, ?>' available: expected single matching bean but found 2: redisTemplate,stringRedisTemplate
猜测可能是匹配到两个bean 故缩小范围
解决方案
1.使用StringRedisTemplate
此处爆红不用理会,正常运行,正常原因是该类型继承自Template<String,String>
同理使用RedisTemplate<String,String>亦可
2.使用RedisTemplate<Object,Object>
如果实在忍受不了爆红可以在@Autowired 后面加(required=false)