Two beans implement the same interface. How do you inject the one you want?
Short answer
Spring resolves beans by type; with several candidates and nothing to pick one, startup fails with NoUniqueBeanDefinitionException. Pick with @Primary, @Qualifier, the parameter name, or inject them all as List<T>/Map<String, T>.
3 points to say:
- Several beans of one type and nothing to choose:
NoUniqueBeanDefinitionExceptionat startup. @Primaryfor the default bean,@Qualifierwhen the caller chooses;@Qualifierbeats@Primary; the parameter name is the last fallback.- Need every implementation: inject a
List/Map(Strategy pattern), ordered with@Order.
Keep reading the deep dive
Create a free account to read the full explanation and follow-up questions, mark questions as studied and review them on a schedule.