The @Qualifier annotation in the Spring framework is used along with the @Autowired annotation. When we have two or more beans of the same type, in order to distinguish between them as to which bean should be injected, we use the @Qualifier annotation.
@Qualifier annotation in Spring Boot Example
Let’s understand this with an example. Say we have an interface Employee having only one abstract method that will return the employee details. Now, we have two employees, one is a developer and one is a manager, both of whom implement the employee interface. If we @Autowire employee interface for loose coupling, the spring will get confused about which bean needs to be injected, the developer bean or the manager bean.
import org.springframework.stereotype.Service;
@Service
public interface Employee {
public String getEmployeeDetails();
}
We will create the Developer and Manager class, implement the Employee interface, and provide the definition for the getEmployeeDetails() method.
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
@Component
@Qualifier("developer")
public class Developer implements Employee {
@Override
public String getEmployeeDetails() {
return "Priya: DEV_ROLE";
}
}
Below is the Manager class that implements the Employee interface and provides the definition for the getEmployeeDetails() method.
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
@Component
@Qualifier("manager")
public class Manager implements Employee {
@Override
public String getEmployeeDetails() {
return "Ravi: MGR_ROLE";
}
}
To run and test our application, we implements CommandLineRunner with our SpringBootApplication class.
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import com.paulsofts.com.employeeservice.data.Employee;
@SpringBootApplication
public class EmployeeServicesApplication implements CommandLineRunner{
/**
* Injecting the Employee interface dependency for loose coupling
* @author paulsofts
*/
@Autowired
private Employee employee;
public static void main(String[] args) {
SpringApplication.run(EmployeeServicesApplication.class, args);
}
/**
* Simply, we are calling the getEmployeeDetails() method with employee instance
* @author paulsofts
*/
@Override
public void run(String... args) throws Exception {
System.out.println(this.employee.getEmployeeDetails());
}
}
Output:
Required a single bean, but 2 were found
We can easily resolve this thing using the @Qualifier annotation along with the @Autowired annotation. Simply, we have to use @Qualifier with the bean name that we want Spring to inject. Here, we have used @Qualifier(“developer”) in order to inject a developer bean.
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import com.paulsofts.com.employeeservice.data.Employee;
@SpringBootApplication
public class EmployeeServicesApplication implements CommandLineRunner{
/**
* 1. Injecting the Employee interface dependency for loose coupling
* 2. Using @Qualifier and specifying the spring to use, developer bean
* @author paulsofts
*/
@Autowired
@Qualifier("developer")
private Employee employee;
public static void main(String[] args) {
SpringApplication.run(EmployeeServicesApplication.class, args);
}
/**
* Simply, we are calling the getEmployeeDetails() method with employee instance
* @author paulsofts
*/
@Override
public void run(String... args) throws Exception {
System.out.println(this.employee.getEmployeeDetails());
}
}
As we can see below, this time Spring is able to distinguish which bean needs to be injected.