Multiple implementations
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. @Primarymarks 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
@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.
Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.