Skip to content
Ameen Abozaid
Planned lessonJNR-009-L04

Multiple implementations

  • Lesson 4 of 8
  • Junior
On This Lesson 4 topics

This lesson is planned. The outline below lists the topics it will cover. The full lesson has not been published yet.

In this lesson

resolve ambiguity with @Primary and @Qualifier.

Planned topics

  • Two beans of the same type make Spring unable to choose: NoUniqueBeanDefinitionException.
  • @Primary marks the default choice.
  • @Qualifier("name") at the injection point picks a specific bean.
  • Injecting a List<PricingStrategy> collects all implementations.
  • Bean names default to the class name with a lowercase first letter.
Draft outline details

Demonstration

java
@Component @Primary class StripePayment implements PaymentProvider {}
@Component("paypal") class PayPalPayment implements PaymentProvider {}

OrderService(@Qualifier("paypal") PaymentProvider payment) { ... }

Check your understanding

You want one default and one exception. Which annotations?

Show answer

@Primary on the default, @Qualifier where the exception is used.

Practice

Apply this lesson with the module's lab, exercises and quiz.

Lab and exercises Quiz

Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.

Continue to next lesson

Type to search levels, phases, modules and lessons.