我的bug案例:
@Resource
private StringRedisTemplate redisTemplate;
public void setFunctionRowNum(String rowType,Integer type) {
redisTemplate.opsForValue().set(this.getRedisKey(type), rowType); //1是双行 0是单行
}
//SpringBoot Bean注入异常,找不到类
Caused by:org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type ‘com.xxx.xxx.xxx’ available
原因(个人总结):
@Resource默认按byName自动注入,因为用的是rediTemplate,所以注入了RedisTemplate类,而非StringRedisTemplate,所以这里会报Bean注入异常
而@Autowired默认按类型装配,类型为StringRedisTemplate,所以注入类型为StringRedisTemplate类。
解决办法(两种):
//将@Resource注解换成@Autowired
@Autowired
private StringRedisTemplate redisTemplate;
public void setFunctionRowNum(String rowType,Integer type) {
redisTemplate.opsForValue().set(this.getRedisKey(type), rowType); //1是双行 0是单行
}
//或者将private StringRedisTemplate redisTemplate 改为private StringRedisTemplate stringRedisTemplate
@Autowired
private StringRedisTemplate stringRedisTemplate;
public void setFunctionRowNum(String rowType,Integer type) {
stringRedisTemplate.opsForValue().set(this.getRedisKey(type), rowType); //1是双行 0是单行
}