TA的每日心情 | 开心 2021-12-13 21:45 |
---|
签到天数: 15 天 [LV.4]偶尔看看III
|
在使用spring自动注入时遇到了org.springframework.beans.factory.NoSuchBeanDefinitionException异常。经检查是因为在bean类中自动注入了本类(写代码把自己写懵逼了)。但也想写一下出现这个异常。
出现这个异常的原因是因为在spring的上下文中找不到相应bean类。
Cause: No qualifying bean of type […] found for dependency:最经常出现的原因是就是这个原因:想要尝试注入的bean类没有定义。
- @Component
- public class A {
-
- @Autowired
- private B b;
- ...
- }
复制代码- org.springframework.beans.factory.NoSuchBeanDefinitionException:
- No qualifying bean of type [com.packageB.B] found for dependency:
- expected at least 1 bean which qualifies as autowire candidate for this dependency.
- Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
复制代码 根据错误的提示可以很清楚的明白了。
需要创建B类,然后在添加@Component, @Repository, @Service, @Controller, etc注解。当然如果你注入的是抽象类或者接口,不需要在此抽象类和接口上添加这些注解。只需要在它的子类上添加上注解就可以了。
|
|