The @Qualifier annotation

Handling multiple autowire candidates with @Primary is more effective when only one primary candidate can be determined for multiple autowire candidates. The @Qualifier annotation gives you more control over the selection process. It allows you to give a reference associated with a specific bean type. That reference can be used to qualify the dependency that needs to be autowired. Let's look at the following code:

@Component
public class AccountService implements CustomerService {

}
@Component
@Qualifier("BankingService")
public class BankingService implements CustomerService {

}

@Component
public class SomeService {

private CustomerService customerService;

@Autowired
@Qualifier("bankingservice")
public BankingService(CustomerService customerService) {
this.customerService = customerService;
}
.....
}

In the previous example, there are two customer services available: BankingService and AccountService; however, due to @Qualifier("bankingservice") being used in the SomeService class, BankingService will be selected for auto wiring.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset