針對RedisTemplate封裝了一個RedisUtils類,在調用時使用@Autowire注入此類,出現報錯,內容如下
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.popmart.testcontainer.testcase.OmsTest':
Unsatisfied dependency expressed through field 'redisUtils'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.popmart.config.redis.RedisUtils' available:
expected at least 1 bean which qualifies as autowire candidate.
Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
解決思路:
1.根據保存分析原因:從報錯信息無法掃描到redisUtils這個bean,如果@Autowire使用注釋進行配置,則可能會缺少cn.itcast.crm.service.BaseDictService實現的注釋(@Service或@Component等)。
2.在RedisUtils類嘗試着增加了@Service或@Component后問題解決,增加了@server注解后springboot啟動可掃描到此bean。