Table of Contents
In this post, we will see about an exception: No qualifying bean of type. Exceptions are least expected but you might get it while working with Spring. or Spring boot.
Did you get this exception: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type?
If yes, let’s see different reasons for it.
Reason 1:
You forgot to declare the bean itself.
Let’s understand this with the help of example.
XML configuration
1. Create a simple java maven project.
2. Maven dependency
put spring and cglib maven dependency in pom.xml.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <!-- cglib required for @configuration annotation --> <dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> <version>3.0</version> </dependency> </dependencies> <properties> <spring.version>4.2.1.RELEASE</spring.version> </properties> |
So your pom.xml will look like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"< <modelVersion<4.0.0</modelVersion> <groupId>org.arpit.java2blog</groupId> <artifactId>SpringHelloWorldJavaBasedConfiguration</artifactId> <version>0.0.1-SNAPSHOT</version> <name>SpringHelloWorldJavaBasedConfiguration</name> <description>It provides hello world program for java based config </description> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <!-- cglib required for @configuration annotation --> <dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> <version>3.0</version> </dependency> </dependencies> <properties> <spring.version>4.2.1.RELEASE</spring.version> </properties> </project> |
3. Create Bean class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
package org.arpit.java2blog; import org.springframework.beans.factory.annotation.Autowired; public class X { @Autowired Y y; public Y getY() { return y; } public void setY(Y y) { this.y = y; } } |
Please note that we are using Autowired annotation to inject bean y in x.
1 2 3 4 5 6 7 8 9 10 11 12 |
package org.arpit.java2blog; public class Y { public void methodY() { System.out.println("In method Y"); } } |
4. ApplicationContext.xml
Create ApplicationContext.xml in src/main/resources as below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <bean id="x" class="org.arpit.java2blog.X"> </bean> <context:annotation-config/> </beans> |
We have just declared one bean x here
5. Create SpringApplicationMain.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package org.arpit.java2blog; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * * Spring Application main */ public class SpringApplicationMain { public static void main( String[] args ) { ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("ApplicationContext.xml"); X firstX = (X) ac.getBean("x"); Y firstY = firstX.getY(); ac.close(); } } |
6. Run it
When you run above program, you will get below output.
.
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.arpit.java2blog.Y] 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)}
.
.
As you can see, we are getting NoSuchBeanDefinitionException: No qualifying bean of type as there is no bean "y" declared in XML.
Java configuration
First 2 steps will remain the same.
let’s add @Component to X.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package org.arpit.java2blog; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component public class X { @Autowired Y y; public Y getY() { return y; } public void setY(Y y) { this.y = y; } } |
1 2 3 4 5 6 7 8 9 10 11 12 |
package org.arpit.java2blog; public class Y { public void methodY() { System.out.println("In method Y"); } } |
4. Create ApplicationConfiguration.java as below.
1 2 3 4 5 6 7 8 9 10 11 |
package org.arpit.java2blog; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; @Configuration @ComponentScan("org.arpit.java2blog") public class ApplicationConfiguration { } |
- Create Main class
12345678910111213package org.arpit.java2blog;import org.springframework.context.ApplicationContext;import org.springframework.context.annotation.AnnotationConfigApplicationContext;;</li></ol><p>public class SpringJavaConfigMain {</p><pre><code>public static void main(String[] args) {@SuppressWarnings("resource")ApplicationContext appContext = new AnnotationConfigApplicationContext(ApplicationConfiguration.class);X x = (X) appContext.getBean("x");Y y = x.getY();}}
- Run it
When you run it, you will get below output..
.
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.arpit.java2blog.Y] 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)}
.
.If you add @Component to Y.java, above exception won’t occur.
Reason 2:
You have not added package name to @ComponentScan.You might be using @Component,@Service or @Repository but there is no entry for package with @ComponentScan.
For example:
If I change package name to "org.arpit.dummy", program won’t work,even if you have put @Component with bean Y.1234567891011package org.arpit.java2blog;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;@Configuration@ComponentScan("org.arpit.dummy")public class ApplicationConfiguration {}
Reason 3:
You have more than one candidate for autowiringLet’s understand this with the help of example.
Let’s say you one interface named "Decorable"1234567package org.arpit.java2blog;public interface Decorable {public void decorate();}This interface is implemented by two classes.
CurtianDecorator.java123456789101112131415package org.arpit.java2blog;import org.springframework.stereotype.Component;@Componentpublic class CurtianDecorator implements Decorable {@Overridepublic void decorate() {System.out.println("Decorating room using curtains");}}WallDecorator.java
12345678910111213141516package org.arpit.java2blog;import org.springframework.stereotype.Component;@Componentpublic class WallDecorator implements Decorable {@Overridepublic void decorate() {System.out.println("Decorating room with wall decorators");}}Create Room.java which will composite reference variable of type interface Decorable.
123456789101112131415161718192021package org.arpit.java2blog;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;@Componentpublic class Room {@AutowiredDecorable decorable;public Decorable getDecorable() {return decorable;}public void setDecorable(Decorable decorable) {this.decorable = decorable;}}4. Create ApplicationConfiguration.java as below.
1234567891011package org.arpit.java2blog;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;@Configuration@ComponentScan("org.arpit.java2blog")public class ApplicationConfiguration {}5. Create Main class
1234567891011121314151617package org.arpit.java2blog;package org.arpit.java2blog;import org.springframework.context.ApplicationContext;import org.springframework.context.annotation.AnnotationConfigApplicationContext;;public class SpringJavaConfigMain {public static void main(String[] args) {@SuppressWarnings("resource")ApplicationContext appContext = new AnnotationConfigApplicationContext(ApplicationConfiguration.class);Room room=(Room)appContext.getBean("room");Decorable decorable = room.getDecorable();decorable.decorate();}}6. Run it
When you run it, you will get below output..
.
.
Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [org.arpit.java2blog.Decorable] is defined: expected single matching bean but found 2: curtianDecorator,wallDecorator
.
.Please note that this time,we are getting NoUniqueBeanDefinitionException because there are two autowired candidates for Decorable in Room class and spring is not able to resolve which one to use.
You can solve this probem using @Qualifier annotation.1234567891011121314151617181920212223package org.arpit.java2blog;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.stereotype.Component;@Componentpublic class Room {@Autowired@Qualifier("wallDecorator")Decorable decorable;public Decorable getDecorable() {return decorable;}public void setDecorable(Decorable decorable) {this.decorable = decorable;}}When you run the program again, you will get below output:
Jun 24, 2018 5:19:36 PM org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@2e817b38: startup date [Sun Jun 24 17:19:36 IST 2018]; root of context hierarchy
Decorating room with wall decoratorsThat’s all about no qual
Was this post helpful?
Let us know if this post was helpful. Feedbacks are monitored on daily basis. Please do provide feedback as that\'s the only way to improve. - Run it