@Qualifier Annotation vs @Primary Annotation with practical example in Spring Boot session 6

In this post, we will be going to explore @Qualifier Annotation and @Primary Annotation.
@Qualifier Annotation
Let discuss the following scenario
I have the following interface. The purpose of this interface is we can implement this interface in any class where we want to display name for different types of Persons.
For Example, I have EmployeeService and CustomerService class which implements the following interface. Whenever I call the “displayByName” from EmployeeService,I want to display the employee name. Similarly CustomerService also.

public interface BaseService {
    public List displayByName(String name);
}

So I implemented BaseServcie interface in EmployeService and CustomerService like below

@Service
public class CustomerService implements BaseService{

    public List displayByName(String name) {
        return dataHandler.displayCustomer(name);
    }
}

@Service
public class EmployeeService implements BaseService {

@Override
public List displayByName(String name) {

return dataHandler.displayEmployee(name);
}
}

Now I implemented this class as per need. According to this design, I am able to fetch the Employee name and Customer name by calling displayByName from BaseService.
But Spring Boot never support this kind of design.

public class CustomerController {
    @Autowired
    private BaseService baseService;
}
public class EmployeeController {
   @Autowired
   private BaseService baseService;
}

If you notice the above code, BaseService is @Autowired both the class. When starting the Spring Boot Application doesn’t know which instance needs to assign in BaseService. Whether EmployeeService or CustomerService? Because BaseService implements two different class so, Spring Boot confuse which reference need to assign in BaseService which declared in EmployeeController and CustomerController. So, SpringBoot throws Ambiguity Error. In order to assign the correct reference to BaseService, We need to use @Qualifier Annotation by doing like below.

public class CustomerController {
    @Autowired
    @Qualifier("customerService")
    private BaseService baseService;
}
public class EmployeeController {
   @Autowired
   @Qualifier("employeeService")
   private BaseService baseService;
}

In the above code, I used @Qualifier Annotation and passing a parameter which says clearly while starting the server pass customerSerivce reference in CustomerController and similarly bypassing employeService I am saying that assign EmployeeService reference in EmployeeControlle. SpringBoot App is more clear now and assign a right reference to the respective Controller class.
@Primary Annotation
Using @Primary Annotation we can define which reference we need to assign in BaseService while starting the Server.

While defining the Service class if you use @Primary Annotation like below. Spring Boot assigns CustomerService reference to BaseService at the time of starting the Server.

@Service
@Primary
public class CustomerService implements BaseService{

    public List displayByName(String name) {
        return dataHandler.displayCustomer(name);
    }
}

Since I used @Primary in Customer Service, I don’t have to use @Qualifier here because it will automatically assign customerService reference in BaseService at the time of starting the Server. This is happening through @Primary Annotation

public class CustomerController {

    @Autowired
    private BaseService baseService;
}

While injecting BaseService in EmployeeController you have to use @Qualifier and need to pass service class name.
According to @Primary annotation, we can tell to Spring Boot which class reference is the default reference at the time of starting the server.

public class EmployeeController {

    @Autowired
    @Qualifier("employeeService")
    private BaseService baseService;
}

For complete Demo Please watch this full video.

Download complete source code from following GitHub location.
https://github.com/sureshstalin/itgarden/tree/master/SpringBoot6


Buy our hygiene Napkin. Our Napkins are chemical-free napkin and it has three categories.
Wings Napkin – This napkin wrapped around pure cotton cloth
https://pinepad.in/product/hygiene-eco-friendly-biodegradable-large-size-pure-cotton-cloth-wrapped-sanitary-napkin/
Wingless Napkin

https://pinepad.in/product/hygiene-eco-friendly-biodegradable-large-size-sanitary-pad-pack-4-32-pads/
Maternity Napkin for new Moms
https://pinepad.in/product/hygiene-eco-friendly-and-biodegradable-maternity-pad-pack-of-4-24-pads/

 

Close Menu
×
×

Cart