Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

Expanded Audience – Frontegg – Security (partner)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

November Discount Launch 2022 – Top
We’re finally running a Black Friday launch. All Courses are 30% off until end-of-day today:

>> GET ACCESS NOW

NPI – Lightrun – Spring (partner)

We rely on other people’s code in our own work. Every day. It might be the language you’re writing in, the framework you’re building on, or some esoteric piece of software that does one thing so well you never found the need to implement it yourself.

The problem is, of course, when things fall apart in production - debugging the implementation of a 3rd party library you have no intimate knowledge of is, to say the least, tricky. It’s difficult to understand what talks to what and, specifically, which part of the underlying library is at fault.

Lightrun is a new kind of debugger.

It's one geared specifically towards real-life production environments. Using Lightrun, you can drill down into running applications, including 3rd party dependencies, with real-time logs, snapshots, and metrics. No hotfixes, redeployments, or restarts required.

Learn more in this quick, 5-minute Lightrun tutorial:

>> The Essential List of Spring Boot Annotations and Their Use Cases

1. Overview

In this tutorial, we discuss the Spring org.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'll illustrate the possible causes for this problem and the available solutions.

And of course, exceptions happen when we least expect them, so have a look at the full list of exceptions and solutions in Spring.

Further reading:

Spring Exceptions Tutorial

Some of the most common exceptions in Spring with examples - why they occur and how to solve them quickly.

Spring BeanCreationException

A quick and practical guide to dealing with different causes of Spring BeanCreationException

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:

@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:

org.springframework.beans.factory.NoSuchBeanDefinitionException: 
No qualifying bean of type [com.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 by classpath 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:

package com.baeldung.packageB;
@Component
public class BeanB { ...}

And the classpath scanning may be configured as follows:

@Configuration
@ComponentScan("com.baeldung.packageA")
public class ContextWithJavaConfig {
    ...
}

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

3. Cause: Field […] in […] Required a Bean of Type […] That Could Not Be Found

In a Spring Boot application for the above scenario, we get a different message.

Let's take the same example where BeanB is wired in BeanA, but it's not defined:

@Component
public class BeanA {
	
    @Autowired
    private BeanB dependency;
    //...
}

If we try to run this simple application, that tries to load BeanA:

@SpringBootApplication
public class NoSuchBeanDefinitionDemoApp {

    public static void main(String[] args) {
        SpringApplication.run(NoSuchBeanDefinitionDemoApp.class, args);
    }
}

The application will fail to start with this error message:

***************************
APPLICATION FAILED TO START
***************************

Description:

Field dependency in com.baeldung.springbootmvc.nosuchbeandefinitionexception.BeanA required a bean of type 'com.baeldung.springbootmvc.nosuchbeandefinitionexception.BeanB' that could not be found.


Action:

Consider defining a bean of type 'com.baeldung.springbootmvc.nosuchbeandefinitionexception.BeanB' in your configuration.

Here com.baeldung.springbootmvc.nosuchbeandefinitionexception is the package for BeanA, BeanB and NoSuchBeanDefinitionDemoApp.

The snippet for this example can be found in this GitHub project.

4. 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.

Let's say an interface IBeanB is implemented by two beans, BeanB1 and BeanB2:

@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:

@Component
public class BeanA {

    @Autowired
    private IBeanB dependency;
    ...
}

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

Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: 
No qualifying bean of type 
  [com.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.

However, notice that in this case the exact exception being thrown is not NoSuchBeanDefinitionException but a subclass: the NoUniqueBeanDefinitionException. This new exception was introduced in Spring 3.2.1 for exactly this reason — to differentiate between the cause where no bean definition was found and where several definitions are found in the context.

Before this change, this was the above exception:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: 
No qualifying bean of type [com.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:

@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).

5. Cause: No Bean Named […] Is Defined

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

@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:

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.

6. Cause: Proxied Beans

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

Because of this, if the bean is injected by an interface, it will be correctly wired in. However, if the bean is injected by the actual class, 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:

@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 okay:

@Service
@Transactional
public class ServiceA implements IServiceA{

    @Autowired
    private IServiceB serviceB;
    ...
}

@Service
@Transactional
public class ServiceB implements IServiceB{
    ...
}

7. Conclusion

This article discussed examples of the possible causes for the common NoSuchBeanDefinitionException — 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.

Finally, the full list of exceptions and solutions in Spring might be a good resource to bookmark.

November Discount Launch 2022 – Bottom
We’re finally running a Black Friday launch. All Courses are 30% off until end-of-day today:

>> GET ACCESS NOW

Generic footer banner
Comments are closed on this article!