博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring NoSuchBeanDefinitionException原因分析
阅读量:4091 次
发布时间:2019-05-25

本文共 4722 字,大约阅读时间需要 15 分钟。

 

概述

在本文中,我将通过实例向你展示Spring 中org.springframework.beans.factory.NoSuchBeanDefinitionException 出现的原因。如果BeanFactory在Spring Context中没有找到bean的实例,就会抛出这个常见的异常。

Cause: No qualifying bean of type […] found for dependency

这个异常的出现一般是因为需要注入的bean未定义 

有一个类BeanA.java

package com.csdn.training.model;@Componentpublic class BeanA {    @Autowired    private BeanB beanB;}

有一个类BeanB.java

package com.csdn.training.service;@Componentpublic class BeanB {}

配置文件applicationContext.xml

用一个测试类去启动拉起Spring容器:

package com.csdn.test;public class AppTest {    public static void main(String[] args) {        ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");        BeanA beanA = (BeanA) context.getBean("beanA");    }}

自动扫描包路径缺少了BeanB,它和BeanA 不在同一路径下 

如果依赖 IBusinessService 在Spring 上下文中没有定义,引导进程报错:No Such Bean Definition Exception.

nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.csdn.training.service.BeanB] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency.

Spring会提示:”Expected at least 1 bean which qualifies as autowire candidate for this dependency“(依赖至少有一个备选的bean能被自动注入) 

出现异常的原因是IBusinessService 在上下文中不存在:如果bean是通过classpath自动扫描来装配,并且IBusinessService已经正确的加上了注解(@Component,@Repository,@Service,@Controller等),也许是你没有把正确的包路径告诉Spring。

配置文件可以如下配置:

如果bean不能自动被扫描到,而手动定义却可以识别,那Bean就没有在Spring上下文中定义。

Cause: No qualifying bean of type […] is defined

造成这一异常的原因可能是Spring上下文中存在两个或以上该bean的定义。如果接口IService 有两个实现类 ServiceImplA 和ServiceImplB 

接口:IService.java

package com.csdn.training.service;public interface IService {}

两个实现类:ServiceImplA.java

package com.csdn.training.service;import org.springframework.stereotype.Service;@Servicepublic class ServiceImplA implements IService {}

ServiceImplB.java

package com.csdn.training.service;import org.springframework.stereotype.Service;@Servicepublic class ServiceImplB implements IService {}

如果BeanA 自动注入这一接口,Spring就无法分辨到底注入哪一个实现类:

package com.csdn.training.model;import com.csdn.training.service.IService;@Componentpublic class BeanA {        @Autowired    private IService serviceImpl;}

然后,BeanFactory就抛出异常NoSuchBeanDefinitionException 

Spring会提示:”expected single matching bean but found 2“(只应该匹配一个bean但是找到了多个)

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.csdn.training.service.IService] is defined: expected single matching bean but found 2: serviceImplA,serviceImplB

上例中,有时你看到的异常信息是NoUniqueBeanDefinitionException,它是NoSuchBeanDefinitionException 它的子类,在Spring 3.2.1中,修正了这一异常,为的是和bean未定义这一异常区分开。

nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [com.csdn.training.service.IService] is defined: expected single matching bean but found 2: serviceImplA,serviceImplB

解决这一异常可以用注解@Qualifier 来指定想要注入的bean的名字。

package com.csdn.training.model;import com.csdn.training.service.IService;@Componentpublic class BeanA {    @Autowired    @Qualifier("serviceImplA")    private IService serviceImpl;}

修改以后,Spring就可以区分出应该注入那个bean的实例,需要注意的是ServiceImplA的默认实例名称是serviceImplA

Cause: No Bean Named […] is defined

当你通过具体的bean的名字去得到一个bean的实例的时候,如果Spring 没有在上下文中找到这个bean,就会抛出这个异常。

package com.csdn.test;public class AppTest {    public static void main(String[] args) {        ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");        context.getBean("beanX");    }}

这个例子中,没有一个bean被定义成 “beanX”,就会抛出如下异常: 

Spring会提示:”No bean named XXX is defined” (没有找到一个名叫XXX的bean)

org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'beanX' is defined at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition

Cause: Proxied Beans

如果bean由JDK的动态代理机制所管理,那么代理将不会继承该bean,它只会实现与其相同的接口。因此,如果bean是通过接口注入的,就可以成功注入。如果通过其实现类注入,Spring就无法将bean实例与类关联,因为代理并不真正的继承于类。 

出现这一原因,很有可能是因为你使用了Spring的事物,在bean上使用了注解@Transactional 
如下,ServiceA注入了ServiceB,这两个service都使用了事物,通过实现类注入bean就不起作用了。 
借口IService.java无变化,其实现类加上事物的注解 
ServiceImplA.java

package com.csdn.training.service;@Service@Transactionalpublic class ServiceImplA implements IService {    @Autowired    private ServiceImplB serviceImplB;}

ServiceImplB.java

package com.csdn.training.service;@Service@Transactionalpublic class ServiceImplB implements IService {}

如果改成通过接口注入,就可以:

ServiceImpl.java

package com.csdn.training.service;@Service@Transactionalpublic class ServiceImplA implements IService {    @Autowired    @Qualifier("serviceImplB")    private IService serviceImplB;}

结论

本文通过几个小例子,分析了NoSuchBeanDefinitionException 用了这一异常可能出现的情形,对于我们在实际开发过程中定位错误提供了一定的参考。 

原文为英文版,我在原文的基础上稍作改动,但又尽量保持原文的精髓,如果对译文有异议,欢迎指正。

转载地址:http://vobii.baihongyu.com/

你可能感兴趣的文章
php性能分析工具xhprof的安装与使用
查看>>
win10安装go语言, 并进行环境配置, 及liteide的简单实用
查看>>
laravel: 集成xhprof
查看>>
laravel: 集成laravel-admin管理后台
查看>>
laravel: 集成laravel/ui, 提供登录注册等功能
查看>>
PHP 7 的五大新特性
查看>>
php使用 memcache 来存储 session
查看>>
php实现socket(转)
查看>>
PHP底层的运行机制与原理
查看>>
php 几个比较实用的函数
查看>>
PHP5各个版本的新功能和新特性总结
查看>>
深入了解php底层机制
查看>>
PHP中的stdClass 【转】
查看>>
XHProf-php轻量级的性能分析工具
查看>>
PHP7新特性 What will be in PHP 7/PHPNG
查看>>
比较strtr, str_replace和preg_replace三个函数的效率
查看>>
ubuntu 下编译PHP5.5.7问题:configure: error: freetype.h not found.
查看>>
PHP编译configure时常见错误 debian centos
查看>>
configure: error: Please reinstall the BZip2 distribution
查看>>
TortoiseGit常用操作
查看>>