@Qualifier annotation example in Spring - onlinetutorialspoint

PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
JAVAEXCEPTIONSCOLLECTIONSSWINGJDBC
EXCEPTIONS
COLLECTIONS
SWING
JDBC
JAVA 8
SPRING
SPRING BOOT
HIBERNATE
PYTHON
PHP
JQUERY
PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
@Qualifier is one of the autowiring annotations in spring.
Sometimes, it may happen that there are two or more beans, each of which equally qualifies to be wired into a property or a parameter. In such cases, Spring does not decide which one is the right one, instead, it may throw NoUniqueBeanDefinitionException. In this case, @Qualifier annotation will control which bean should be wired on a property.
In order to resolve the exception, we can use @Qualifier annotation along with the autowiring annotation.
Here is the complete example, of @Qualifier annotation usage.
pom.xml
<dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring-framework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4j.version}</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>
College.java
package com.onlinetutorialspoint.beans;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

public class College {
    private int collegeId;
    private String collegeName;
    @Autowired
    @Qualifier("cse")
    private Department department;

    public int getCollegeId() {
        return collegeId;
    }

    public void setCollegeId(int collegeId) {
        this.collegeId = collegeId;
    }

    public String getCollegeName() {
        return collegeName;
    }

    public void setCollegeName(String collegeName) {
        this.collegeName = collegeName;
    }

    public Department getDepartment() {
        return department;
    }

    public void setDepartment(Department department) {
        this.department = department;
    }

}
Department.java
package com.onlinetutorialspoint.beans;

public class Department {
    private int deptId;
    private String deptName;

    public int getDeptId() {
        return deptId;
    }

    public void setDeptId(int deptId) {
        this.deptId = deptId;
    }

    public String getDeptName() {
        return deptName;
    }

    public void setDeptName(String deptName) {
        this.deptName = deptName;
    }

}
spring configuration.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-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
    <context:component-scan base-package="com.onlinetutorialspoint.beans" />
    <context:annotation-config />
    <bean id="college" class="com.onlinetutorialspoint.beans.College">
        <property name="collegeId" value="10059" />
        <property name="CollegeName" value="CBIT" />
    </bean>
    <bean id="cse" class="com.onlinetutorialspoint.beans.Department">
        <property name="deptName" value="CSE" />
        <property name="deptId" value="101" />
    </bean>
    <bean id="it" class="com.onlinetutorialspoint.beans.Department">
        <property name="deptName" value="INFORMATION TECHNOLOGY" />
        <property name="deptId" value="102" />
    </bean>
    <bean id="mechanical" class="com.onlinetutorialspoint.beans.Department">
        <property name="deptName" value="Mechanical" />
        <property name="deptId" value="103" />
    </bean>
</beans>
On the above configuration file, we took 3 different Department beans with different ids. To avoid ambiguity, while injecting the dependency of Department bean we can use @Qualifier annotation on top of the property.
Main.java
package com.onlinetutorialspoint.beans;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {

    public static void main(String[] args) {
        BeanFactory beanFactory = new ClassPathXmlApplicationContext("springconfiguration.xml");
        College college = (College) beanFactory.getBean("college");
        Department department = college.getDepartment();
        System.out.println("College Name : "+college.getCollegeName());
        System.out.println("Department Id : "+department.getDeptId());
        System.out.println("Department Name : "+department.getDeptName());
    }

}
Output :
College Name : CBIT
Department Id : 101
Department Name : CSE
Happy Learning 🙂


Spring @Qualifier Complete Example

File size: 15 KB
Downloads: 876


@Autowired,@Qualifier,@Value annotations in Spring
Spring Collection Dependency List Example
Spring Java Configuration Example
Spring Collection Map Dependency Example
Spring Bean Autowire By Constructor Example
Spring Bean Autowire ByName Example
Types of Spring Bean Scopes Example
Spring Bean Autowire ByType Example
How to Get All Spring Beans Details Loaded in ICO
spring expression language example
@Component,@Service,@Repository,@Controller in spring
BeanFactory Example in Spring
Spring AOP Around Advice Example XML
Spring MVC Login Form Example Tutorials
Spring JdbcTemplate CRUD Application

@Autowired,@Qualifier,@Value annotations in Spring
Spring Collection Dependency List Example
Spring Java Configuration Example
Spring Collection Map Dependency Example
Spring Bean Autowire By Constructor Example
Spring Bean Autowire ByName Example
Types of Spring Bean Scopes Example
Spring Bean Autowire ByType Example
How to Get All Spring Beans Details Loaded in ICO
spring expression language example
@Component,@Service,@Repository,@Controller in spring
BeanFactory Example in Spring
Spring AOP Around Advice Example XML
Spring MVC Login Form Example Tutorials
Spring JdbcTemplate CRUD Application






vasanth
May 29, 2016 at 5:26 pm - Reply 

Hello Chanrasekar
Tutorial you very simple , and very clear example .. very easy to understand .. i red all the article,
Thanks for your contribution. 
could you please share  Bigdata – Hadoop concepts









Abhay patel
April 3, 2017 at 9:19 am - Reply 

very useful tutorials and  easy
thank you admin










vasanth
May 29, 2016 at 5:26 pm - Reply 

Hello Chanrasekar
Tutorial you very simple , and very clear example .. very easy to understand .. i red all the article,
Thanks for your contribution. 
could you please share  Bigdata – Hadoop concepts




Hello Chanrasekar
Tutorial you very simple , and very clear example .. very easy to understand .. i red all the article,
Thanks for your contribution. 
could you please share  Bigdata – Hadoop concepts





Abhay patel
April 3, 2017 at 9:19 am - Reply 

very useful tutorials and  easy
thank you admin




very useful tutorials and  easy
thank you admin