How to Use @Autowired and @Qualifier in Spring

In this tutorial, you will learn how to use the @Autowired and @Qualifier annotations together to auto wire a specific Bean. Sometimes you might have more than one Bean of the same type and if you do not explicitly specify which one you would like to auto wire, you will get an NoUniqueBeanDefinitionException when starting up your application.

Let’s have a look at an example which will work very well if @Qualifier annotation is used together with @Autowired and will throw an NoUniqueBeanDefinitionException if the @Qualifier annotation is not used.

Create Service Interface

Let’s create an EmployeeService interface with only one method. This interface will be implemented by two different Service classes in the following section.

package com.appsdeveloperblog.examples.qualifier.QualifierAnnotation.service;

import com.appsdeveloperblog.examples.qualifier.QualifierAnnotation.model.Employee;
import java.util.List;

public interface EmployeeService {
    List<Employee> getEmployees();
}

Implement Service Interface

Lets now create two Service classes which will implement the above mentioned EmployeeService interface.

Please note that both of the Service classes below specify the logical service name. For the ManagerServiceImpl I have used the managerService and for the DeveloperServiceImpl I have used the developerService. 

ManagerServiceInterface

package com.appsdeveloperblog.examples.qualifier.QualifierAnnotation.service;

import com.appsdeveloperblog.examples.qualifier.QualifierAnnotation.dao.DAO;
import com.appsdeveloperblog.examples.qualifier.QualifierAnnotation.model.Employee;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

@Service("managerService")
public class ManagerServiceImpl implements EmployeeService {

    @Autowired
    @Qualifier("MySQL")        
    DAO database;
    
    @Override
    public List<Employee> getEmployees() {
        
        database.init();
        
        List<Employee> returnValue = database.getManagers();
        
        return returnValue;
    }
    
}

DeveloperService Interface

package com.appsdeveloperblog.examples.qualifier.QualifierAnnotation.service;

import com.appsdeveloperblog.examples.qualifier.QualifierAnnotation.dao.DAO;
import com.appsdeveloperblog.examples.qualifier.QualifierAnnotation.model.Employee;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

@Service("developerService")
public class DeveloperServiceImpl implements EmployeeService {
 
    @Autowired
    @Qualifier("MySQL")        
    DAO database;
    
    @Override
    public List<Employee> getEmployees() {
        
        database.init();
        
        List<Employee> returnValue = database.getDevelopers();
        
        return returnValue;
    }
    
}

Use @Qualifier to Differentiate Beans in RestController

Now let’s see how we can auto wire both of the above mentioned Service classes into a Rest Controller Class.

Please note the use of @Qualifier annotation which helps to differentiate between the Beans which are being auto wired. If the @Qualifier annotation is removed, then Spring framework will not be able to figure out which of the EmployeeService implementations need to be auto wired and will throw an error message.

package com.appsdeveloperblog.examples.qualifier.QualifierAnnotation.controller;

import com.appsdeveloperblog.examples.qualifier.QualifierAnnotation.model.Employee;
import com.appsdeveloperblog.examples.qualifier.QualifierAnnotation.service.EmployeeService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/employees")
public class EmployeeController {
    
    @Autowired
    @Qualifier("developerService")
    EmployeeService dService;
    
    @Autowired
    @Qualifier("managerService")
    EmployeeService mService;
    
    @GetMapping(path="/developers")
    public List<Employee> getDevelopers() {
       return dService.getEmployees();
    }
    
    @GetMapping(path="/managers")
    public List<Employee> getManagers() {
       return mService.getEmployees();
    }
    
}

I hope this tutorial on how to use the @Autowired and @Qualifier annotations together was helpful for you. If you would like to learn more about how to build RESTful Web Services with Spring Framework please check the Spring MVC and Spring Boot section of this site. And if you enjoy learning by watching a series of step by step video lessons, then have a look at the below list of video courses that teach Spring Framework.