Skip to content
Sharp

Dependency Injection · interview question

How does Spring Boot handle dependency injection when multiple beans match the type being injected?

Multiple choiceMediumchapter-9-curatedpremiumdependency-injectionconstructor-vs-field-injection
Choose one answer
Answer and explanation

Correct answer

D. Uses the @Primary annotation

Why

When multiple beans match the type being injected, Spring Boot uses the @Primary annotation to determine which bean to inject. If no @Primary is defined and ambiguity persists, an exception is thrown.

Test yourself on Dependency Injection

A free test with instant, explained results. No account needed.

Start free test
All Dependency Injection questions →