Bird
0
0

How do you inject paypalPaymentService into a client class using constructor injection with @Qualifier?

hard📝 Application Q8 of 15
Spring Boot - Inversion of Control and Dependency Injection
You have two beans of type PaymentService: @Component("creditCardPaymentService") and @Component("paypalPaymentService"). How do you inject paypalPaymentService into a client class using constructor injection with @Qualifier?
Apublic Client(@Qualifier PaymentService paymentService) { this.paymentService = paymentService; }
Bpublic Client(@Autowired PaymentService paymentService) { this.paymentService = paymentService; }
Cpublic Client(PaymentService paymentService) { this.paymentService = paymentService; }
Dpublic Client(@Qualifier("paypalPaymentService") PaymentService paymentService) { this.paymentService = paymentService; }
Step-by-Step Solution
Solution:
  1. Step 1: Use constructor injection

    Constructor injection requires the dependency to be passed as a parameter.
  2. Step 2: Specify bean with @Qualifier

    Use @Qualifier("paypalPaymentService") to specify which bean to inject.
  3. Final Answer:

    public Client(@Qualifier("paypalPaymentService") PaymentService paymentService) { this.paymentService = paymentService; } -> Option D
  4. Quick Check:

    Is @Qualifier used with bean name in constructor? [OK]
Quick Trick: Use @Qualifier with bean name in constructor parameter [OK]
Common Mistakes:
  • Omitting @Qualifier when multiple beans exist
  • Using @Qualifier without bean name
  • Relying on @Autowired alone causing ambiguity

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes