. Advertisement .
..3..
. Advertisement .
..4..
“Org.springframework.beans.factory.nosuchbeandefinitionexception: no qualifying bean of type” is a fairly common question every programmer will encounter. So, what are our options? Everything will be explained to you in this article. Read on it.
What does the error “Org.springframework.beans.factory.nosuchbeandefinitionexception: no qualifying bean of type” happen?
When you run your program, you easily get the following warning message:
org.springframework.beans.factory.nosuchbeandefinitionexception: no qualifying bean of type
You have encounter this error because of many reasons:
The most frequent reason of this exception is that you simply attempt to inject a bean that is not specified.
For instance:
@Component
public class BeanA {
@Autowired
private BeanB dependency;
//...
}
In the above code, BeanB is wiring BeanA like a collaborator. The bootstrap procedure will fail with the no such bean definition exception if the dependency BeanB is not defined in the Spring Context:
org.springframework.beans.factory.NoSuchBeanDefinitionException:
No qualifying bean of type [com.ittutoria.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)}
Spring makes it very obvious why: expected at least one bean that is an autowire candidate for this dependency. If BeanB is correctly annotated as a bean (@Component, @Repository, @Service, @Controller, etc.) and beans are automatically picked up by classpath scanning, one reason BeanB might not exist in the context is that it might be created in a package that is not scanned by Spring:
package com.ittutoria.packageB;
@Component
public class BeanB { ...}
Additionally, the classpath scanning could be set up as follows:
@Configuration
@ComponentScan("com.ittutoria.packageA")
public class ContextWithJavaConfig {
...
}
BeanB is simply not specified in the current Spring Context if beans are manually defined rather than automatically scanned.
Another reason of this issue is that the context contains two bean definitions rather than just one.
How to solve the error “Org.springframework.beans.factory.nosuchbeandefinitionexception: no qualifying bean of type” ?
Solution 1: Use the @Component annotation
“Anticipated at least one bean which qualifies as an autowire candidate”, reads the error message. The injection class should be added to the ApplicationContext using the @Component annotation. The annotation @Component must be added to the Lion class to resolve the exception “NoSuchBeanDefinitionException: No qualifying bean of type”. The @Component annotation is used to fix an exception in the example below.
package com.yawintutor;
import org.springframework.stereotype.Component;
@Component
public class Lion {
// .....
}
Solution 2: Use the @Qualifier annotation
Using the @Qualifier annotation is not a bad solution for the error “Org.springframework.beans.factory.nosuchbeandefinitionexception: no qualifying bean of type“. It can be used to specify corectly the bean’s name which you want to wire. Look at the following example:
@Component
public class BeanA {
@Autowired
@Qualifier("beanB2")
private IBeanB dependency;
...
}
Solution 3: Use the @ComponentScan annotation
The annotation @ComponentScan must be added to the Zoo class to resolve the error “Org.springframework.beans.factory.nosuchbeandefinitionexception: no qualifying bean of type”. The @ComponentScan annotation is used in the example below to repair an exception.
package com.yawintutor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Component;
@Component
@ComponentScan("com.yawin")
public class Zoo {
@Autowired
Lion lion;
}
Solution 4: Create a class that implements the interface
The spring boot context does not support the implementation class. Create a class that implements the interface and makes it accessible in the ApplicationContext as a spring boot bean. The Animal class’s implementation class is displayed in the example below.
package com.yawintutor;
import org.springframework.stereotype.Component;
@Component
public class Lion implements Animal{
@Override
public String getName() {
return "Lion";
}
}
Conclusion
Individual solutions provided in this article are some of the most fundamental for anyone faced with the error “Org.springframework.beans.factory.nosuchbeandefinitionexception: no qualifying bean of type“. We believe that you can easily solve this error with the above solutions. If you still need assistance or have any questions, a growing community of people are usually willing to assist you. In addition, we anticipate a more creative day filled with new ideas and code.
Read more
Leave a comment