org.springframework.beans.factory. NoUniqueBeanDefinitionException Solution

(600 Views)


In this article, we are discussing the Spring org.springframework.beans. factory.NoUniqueBeanDefinitionException - this is a common exception thrown by the BeanFactory when it finds two beans of same type in the Spring Context.

Description of Exception:

Exception is thrown when a BeanFactory is asked for a bean instance for which multiple matching candidates have been found when only one matching bean was expected.

Complete Exception Stack Trace:

Exception in thread "main" org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'dao.DemoBean#0' defined in class path resource [applicationContext.xml]: Unsatisfied dependency expressed through bean property 'dao'; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'dao.DaoBean' available: expected single matching bean but found 2: daoBean,dao at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireByType(AbstractAutowireCapableBeanFactory.java:1439) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1326) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:578) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:501) at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:760) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:869) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:144) at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:85) at test.Test.main(Test.java:11) Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'dao.DaoBean' available: expected single matching bean but found 2: demoBean,dao at org.springframework.beans.factory.config.DependencyDescriptor.resolveNotUnique(DependencyDescriptor.java:215) at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1116) at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1065) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireByType(AbstractAutowireCapableBeanFactory.java:1424) ... 13 more

Spring Configuration File:

<!-- applicationContext.xml --> <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- bean definitions here --> <bean id="daoBean" class="dao.DaoBean"> <property name="name" value="spring in action"/> </bean> <bean id="dao" class="dao.DaoBean"> <property name="name" value="spring in action"/> </bean> </beans>

DaoBean Class:

public class DaoBean { public String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public void getDataFromDb() { System.out.println("Getting Data From Db"); } }

Client Code:

import org.springframework.context.support.ClassPathXmlApplicationContext; import dao.DaoBean; public class Test { public static void main(String[] args) { try(ClassPathXmlApplicationContext container = new ClassPathXmlApplicationContext("applicationContext.xml");) { DaoBean dao = container.getBean(DaoBean.class); dao.add(); System.out.println("printing name "+dao.name); } } }

Cause Of Exception:

All code is okay but if we look into the applicationContext.xml file , we can easily see that we are cofiguring DaoBean class two times, though it will not give you any compilation error but at runtime in spring context there would be two instances of same bean i.e. daoBean and dao that is the cause of exception , spring context is in between two beans and context is unable to inject the right bean in the client code

Solution:

Just remove any one bean tag from applicationContext.xml file as mentioned below, and all code will work fine.

<!-- applicationContext.xml --> <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- bean definitions here --> <bean id="daoBean" class="dao.DaoBean"> <property name="name" value="spring in action"/> </bean> </beans>

If we do not want to change the applicationContext.xml file, We can use '@Qualifier' annotation for injecting DemoBean in our client code.

If you have any other doubt, feel free to ask us in the comments section, we will try to solve it too.

Solution Worked 1 UpvotesUpvote

        

Solution Didn't Worked 0 DownvotesDownvote



Comments



Search

Play 2048 Game Online

Search Tags