异常org.springframework.beans.factory.NoSuchBeanDefinitionException

原文地址为: 异常org.springframework.beans.factory.NoSuchBeanDefinitionException

在使用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注解。当然如果你注入的是抽象类或者接口,不需要在此抽象类和接口上添加这些注解。只需要在它的子类上添加上注解就可以了。


转载请注明本文地址: 异常org.springframework.beans.factory.NoSuchBeanDefinitionException

猜你喜欢

转载自blog.csdn.net/dearbaba_8520/article/details/80648228