解决SpringBoot Bean注入异常,找不到类的问题

关键报错信息:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.xxx.xxx.User' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

排错步骤:

1.查看Spring找不到的这个类是否加上了对应注解,交由Spring框架管理,例如@Component @Controller @Service @Repository @Mapper等

2.如果该类不在SpringBoot启动类的同级包或子包中,检查是否加入了扫描包注解@ComponentScan,并扫描了该类所涉及的包。

以上两点确认都是正常无误后,还是出现同样的错误,那么有一点很容易忽略可以再次检查:

3.全局搜索该类的类名或者加上注解的别名,查看是否有其他Bean有相同名字,例如如下写法就会造成相同Bean的别名,首先创建的Bean,不会报错,第二个相同Bean名创建时,就开始报上诉错误了:

 保证没有相同Bean名后,错误也就不再报了。

原文地址:https://www.cnblogs.com/qukun/p/14043356.html