Wednesday , January 17 2018
Home / Spring / @Qualifier annotation example in Spring

@Qualifier annotation example in Spring

@Qualifier is one of the autowiring annotations in spring. @Qualifier annotation can be used to control which bean should be auto wired on a property.

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 annnotation usage.

@Qualifier annotation Example :

Project Structure :

@Qualifier Example

Required Dependencies :



Spring Beans :

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;
    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;


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 version="1.0" encoding="UTF-8"?>
<beans xmlns=""
    xmlns:xsi="" xmlns:context=""
    <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 id="cse" class="com.onlinetutorialspoint.beans.Department">
        <property name="deptName" value="CSE" />
        <property name="deptId" value="101" />
    <bean id="it" class="com.onlinetutorialspoint.beans.Department">
        <property name="deptName" value="INFORMATION TECHNOLOGY" />
        <property name="deptId" value="102" />
    <bean id="mechanical" class="com.onlinetutorialspoint.beans.Department">
        <property name="deptName" value="Mechanical" />
        <property name="deptId" value="103" />
On the above configuration file, we took 3 different Department beans with different ids. To avoid the ambiguity, while injecting the dependency of Department bean we can use @Qualifier annotation on top of the property.

Run the application :

package com.onlinetutorialspoint.beans;

import org.springframework.beans.factory.BeanFactory;

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

Download Example

About chandrashekhar

you have reach this so far, that shows you like what you are learning. Then why don’t you support us to improve for bettor tutorials by leaving your valuable comments and why not you keep in touch with us for latest updates on your favorite blog @ facebook , twitter , Or Google+ ,


Spring Bean Autowire ByType Example

In this tutorial, we are going to learn about one of the autowire strategy in …



    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


    very useful tutorials and easy

    thank you admin

Leave a Reply

Your email address will not be published. Required fields are marked *