How to Use @Autowired and @Qualifier in Spring

In this tutorial, you will learn how to use the @Autowired and @Qualifier annotations to inject a specific instance of a Bean. Sometimes you might have more than one implementation of an interface. If you do not explicitly specify which one you would like to inject, you will get a NoUniqueBeanDefinitionException when starting your application.

Let’s look at an example that will work very well if @Qualifier annotation is used with @Autowired and will throw a 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 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

Let’s now create two Service classes to implement the above-mentioned EmployeeService interface.

Please note that both 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 the 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 that are being auto-wired. If the @Qualifier annotation is removed, then the Spring framework cannot figure out which 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 want to learn more about building RESTful Web Services with Spring Framework, please check the Spring Boot tutorials page. And if you enjoy learning by watching a series of step-by-step video lessons, then look at the below list of video courses that teach Spring Framework.


Leave a Reply

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