Saturday, 1 June 2019

Spring | Spring Autowiring @Qualifier


There may be a situation when you create more than one bean of the same type and want to wire only one of them with a property, in such case you can use @Qualifier annotation along with @Autowired to remove the confusion by specifying which exact bean will be wired.
Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [spring.core.qualifier.PastInformation] is defined: expected single matching bean but found 2: two,one at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:970) at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:858) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:480) ... 15 more

autowiring-spring.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"xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-4.0.xsd"

       default-autowire="byName" default-autowire-candidates="*">

       <context:annotation-config />

       <bean id="employee" class="spring.core.qualifier.Employee">
              <property name="id" value="1"></property>
              <property name="name" value="Rajesh kumar"></property>
              <property name="profile" value="developer"></property>
       </bean>

       <bean id="two" class="spring.core.qualifier.PastInformation">
              <property name="lastCompany" value="ABC2"></property>
       </bean>
      
       <bean id="one" class="spring.core.qualifier.PastInformation">
              <property name="lastCompany" value="ABC1"></property>
       </bean>
</beans>

PastInformation.java
package spring.core.qualifier;
public class PastInformation {
      
       private String lastCompany;

       public void displayInfo(String seq) {
             
              String empDetails = seq + " : "+lastCompany;
              System.out.println(empDetails);
             
       }
       /** setters and getters */
}


Employee.java
package spring.core.qualifier;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

public class Employee {
       private int id;
       private String name;
       private String profile;

       @Autowired
       @Qualifier("one")
       private PastInformation lastOne;

       @Autowired
       @Qualifier("two")
       private PastInformation lastSecond;

       public void displayInfo() {
              lastOne.displayInfo("first");
              lastSecond.displayInfo("last");
              String empDetails = "id : "+id+" name :"+name+" profile : " + profile;
              System.out.println(empDetails);
       }
       /** setters and getters */
}


TestIOC.java
package spring.core.qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestIOC {
       private static ApplicationContext appContext;
       public static void main(String[] args) {
              appContext = new ClassPathXmlApplicationContext     
                           ("spring/core/qualifier/autowiring-spring.xml");
              Employee empl1 = appContext.getBean("employee", Employee.class);
              empl1.displayInfo();
       }
}

Output
first: ABC1
last: ABC2
id: 1 name : Rajesh kumar profile: developer

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...