1. Overview

In this article, we are discussing the Springorg.springframework.beans.factory.NoSuchBeanDefinitionException – this is a common exception thrown by the BeanFactory when trying to resolve a bean that simply isn’t defined in the Spring Context.

We will discuss here the possible causes for this problem and the available solutions.

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

The most common cause of this exception is simply trying to inject a bean that isn’t defined. For example – BeanB is wiring in a collaborator – BeanA:

1
2
3
4
5
6
7
@Component
public class  BeanA {
 
    @Autowired
    private BeanB dependency;
    ...
}

Now, if the dependency – BeanB – is not defined in the Spring Context, the bootstrap process will fail with the no such bean definition exception:

1
2
3
4
org.springframework.beans.factory.NoSuchBeanDefinitionException:
    No qualifying bean of type [org.baeldung.packageB.BeanB] 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)}

The reason is clearly indicated by Spring: “expected at least 1 bean which qualifies as autowire candidate for this dependency“

One reason BeanB may not exist in the context – if beans are picked up automatically byclasspath scanning, and if BeanB is correctly annotated as a bean (@Component,@Repository, @Service, @Controller, etc) – is that it may be defined in a package that is not scanned by Spring:

1
2
3
package org.baeldung.packageB;
@Component
public class  BeanB { ...}

While the classpath scanning may be configured as follows:

1
2
3
4
5
@Configuration
@ComponentScan("org.baeldung.packageA")
public class  ContextWithJavaConfig {
    ...
}

If beans are not automatically scanned by instead defined manually, then BeanB is simply not defined in the current Spring Context.

3. Cause: No qualifying bean of type [...] is defined

Another cause for the exception is the existence of two bean definitions in the context, instead of one. For example, if an interface – IBeanB is implemented by two beans –BeanB1 and BeanB2:

1
2
3
4
5
6
7
8
@Component
public class  BeanB1 implements IBeanB {
    //
}
@Component
public class  BeanB2 implements IBeanB {
    //
}

Now, if BeanA autowires this interface, Spring will not know which one of the two implementations to inject:

1
2
3
4
5
6
7
@Component
public class  BeanA {
 
    @Autowired
    private IBeanB dependency;
    ...
}

And again, this will result in a NoSuchBeanDefinitionException being thrown by theBeanFactory:

1
2
3
Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: 
No qualifying bean of type [org.baeldung.packageB.IBeanB] is defined: 
expected single matching bean but found 2: beanB1,beanB2

Similarly, Spring clearly indicates the reason for the wiring failure: “expected single matching bean but found 2″.

Notice however, that in this case, the exact exception being thrown is notNoSuchBeanDefinitionException but a subclass – theNoUniqueBeanDefinitionException. This new exception has been introduced in Spring 3.2.1, for exactly this reason – to differentiate between the cause where no bean definition was found and this one – where several definitions are found in the context.

Before this change, the exception above was:

1
2
3
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: 
No qualifying bean of type [org.baeldung.packageB.IBeanB] is defined: 
expected single matching bean but found 2: beanB1,beanB2

One solution to this problem is to use the @Qualifier annotation to specify exactly the name of the bean we want to wire:

1
2
3
4
5
6
7
8
@Component
public class  BeanA {
 
    @Autowired
    @Qualifier("beanB2")
    private IBeanB dependency;
    ...
}

Now Spring has enough  information to make the decision of which bean to inject –BeanB1 or BeanB2 (the default name of BeanB2 is beanB2).

4. Cause: No Bean Named [...] is defined

A NoSuchBeanDefinitionException may also be thrown when a bean that isn’t defined isrequested by name from the Spring context:

1
2
3
4
5
6
7
8
9
10
11
@Component
public class  BeanA implements InitializingBean {
 
    @Autowired
    private ApplicationContext context;
 
    @Override
    public void  afterPropertiesSet() {
        context.getBean("someBeanName");
    }
}

In this case, there is no bean definition for “someBeanName” – leading to the following exception:

1
2
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: 
No bean named 'someBeanName' is defined

Again, Spring clearly and concisely indicates the reason for the failure: “No bean named X is defined“.

5. Cause: Proxied Beans

When a bean in the context is proxied using the JDK Dynamic Proxy mechanism, then the proxy will not extend the target bean (it will however implement the same interfaces).

Because of this, if the bean is injected by an interface, it will be correctly wired in. If however the bean is injected by the actual class, then Spring will not find a bean definition that matches the class – since the proxy does not actually extend the class.

A very common reason the bean may be proxied is the Spring transactional support – namely beans that are annotated with @Transactional.

For example, if ServiceA injects ServiceB, and both services are transactional, injecting by the class definition will not work:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@Service
@Transactional
public class  ServiceA implements IServiceA{
 
    @Autowired
    private ServiceB serviceB;
    ...
}
 
@Service
@Transactional
public class  ServiceB implements IServiceB{
    ...
}

The same two services, this time correctly injecting by the interface, will be OK:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@Service
@Transactional
public class  ServiceA implements IServiceA{
 
    @Autowired
    private IServiceB serviceB;
    ...
}
 
@Service
@Transactional
public class  ServiceB implements IServiceB{
    ...
}

6. Conclusion

This tutorial discussed examples of the possible causes for the commonNoSuchBeanDefinitionException – with a focus on how to address these exceptions in practice.

The implementation of all these exceptions examples can be found in the github project – this is an Eclipse based project, so it should be easy to import and run as it is.

Spring NoSuchBeanDefinitionException六大原因总结的更多相关文章

  1. Spring NoSuchBeanDefinitionException原因分析

    摘要:摘要:本文译自EugenParaschiv文章SpringNoSuchBeanDefinitionException原文链接:/2014th7cj/d/file/p/20161012/dv5o0 ...

  2. Node.js深受欢迎的六大原因

    Node.js是一种后起的优秀服务器编程语言,它用来构建和运行Web应用,这和ASP.NET,Ruby on Rails或Spring框架做的工作是类似的.它使用JavaScript作为主要的开发语言 ...

  3. Spring NoSuchBeanDefinitionException

    转http://www.baeldung.com/spring-nosuchbeandefinitionexception 1. Overview In this article, we are di ...

  4. 《超实用的Node.js代码段》连载三:Node.js深受欢迎的六大原因

    <超实用的Node.js代码段>连载一:获取Buffer对象字节长度 <超实用的Node.js代码段>连载二:正确拼接Buffer Node.js是一种后起的优秀服务器编程语言 ...

  5. Spring循环依赖原因及如何解决

    浅谈Spring解决循环依赖的三种方式 SpringBoot构造器注入循环依赖及解决 原文:https://www.baeldung.com/circular-dependencies-in-spri ...

  6. 选择使用Spring框架的原因(Spring框架为企业级开发带来的好处有哪些)

  7. 分析spring事务@Transactional注解在同一个类中的方法之间调用不生效的原因及解决方案

    问题: 在Spring管理的项目中,方法A使用了Transactional注解,试图实现事务性.但当同一个class中的方法B调用方法A时,会发现方法A中的异常不再导致回滚,也即事务失效了. 当这个方 ...

  8. 现在就使用HTML5的十大原因

    你难道还没有考虑使用HTML5? 当然我猜想你可能有自己的原因: 它现在还没有被广泛的支持,在IE中不好使,或者你就是喜欢写比较严格的XHTML代码. HTML5是Web开发世界的一次重大的改变,事实 ...

  9. 使用HTML5的十大原因

    你难道还没有考虑使用HTML5? 当然我猜想你可能有自己的原因:它现在还没有被广泛的支持,在IE中不好使,或者你就是喜欢写比较严格的XHTML代码.HTML5是web开发世界的一次重大的改变,事实上不 ...

随机推荐

  1. mysql 查询排名 返回值拼接的骚操作

    故事背景: 依照原系统的框子搞一个新的系统给其他人使用,因为新的系统配置库依然需要使用原有的表,表中有字段区分新的系统,然后就有了这个很没劲的数据同步. 难点:配置库码表数据的主键之前是自增,后来改造 ...

  2. 主流框架的搭建(VUE,React)

    vue脚手架:cnpm install vue vue-cli -gvue init webpack/webpack-simple shuaige(新建文件夹的名字)cd shuaigecnpm in ...

  3. C# 让String.Contains忽略大小写

    在C#里,String.Contains是大小写敏感的,所以如果要在C#里用String.Contains来判断一个string里是否包含一个某个关键字keyword,需要把这个string和这个ke ...

  4. [Day6]引用数据类型、ArrayList 集合

    1.类的定义与使用 (1)类的定义格式 创建java文件,与类名相同 public class 类名{ 数据类型  属性名称1: 数据类型  属性名称2: … } (2)使用格式 导包:我们将所有的类 ...

  5. nginx基本用法和HTTPS配置

    nginx作用讲解:1.反向代理:需要多个程序共享80端口的时候就需要用到反向代理,nginx是反向代理的一种实现方式.2.静态资源管理:一般使用nginx做反向代理的同时,应该把静态资源交由ngin ...

  6. Luogu4451 [国家集训队]整数的lqp拆分

    题目链接:洛谷 题目大意:求对于所有$n$的拆分$a_i$,使得$\sum_{i=1}^ma_i=n$,$\prod_{i=1}^mf_{a_i}$之和.其中$f_i$为斐波那契数列的第$i$项. 数 ...

  7. vue 中使用 axios 请求接口,请求会发送两次问题

    在开发项目过程中,发现在使用axios调用接口都会有两个请求,第一个请求时,看不到请求参数,也看不到请求的结果:只有第二次请求时才会有相应的请求参数以及请求结果: 那为甚么会有这么一次额外的请求呢,后 ...

  8. jsr-303 参数校验-学习(转)

    1.是什么? JSR303 是一套 JavaBean 参数校验的标准,它定义了很多常用的校验注解,比如: ----------------------------------------------- ...

  9. 更多more 123123循环

  10. vue 的进度条组件

    先看效果: 要想实现如上图的,进度跳效果,有两种方式,首先介绍第一种: 1.自己用 div 写一个,代码如下 <template> <div class="mfc-slid ...