Spring Boot Quiz: Which Bean Gets Injected?
A Bean Injection Mistake That Can Break Production
Scenario
You're working on a payment service where multiple payment providers exist.
Everything compiles successfully.
What does Spring inject into
paymentService?
A. StripePaymentService
B. PaypalPaymentService
C. Spring randomly picks one implementation.
D. Nothing—the application fails to start because multiple beans match PaymentService.
📢 Get actionable Java and Spring Boot insights every week, including practical code tips and real-world, use-case-based interview questions, to help you level up your backend skills—join 7800+ subscribers for hand-crafted, no-fluff content.
First 100 paid subscribers will get the annual membership at $50/year forever that is ~ $4/mo ( 93 already converted to paid, 7 remaining)
Testimonials
Answer
D. Nothing—the application fails to start because multiple beans match PaymentService.
Typical Startup Error
Parameter 0 of constructor in CheckoutService required a single bean,
but 2 were found:
- stripePaymentService
- paypalPaymentServiceWhy?
Spring performs dependency injection by type.
When it finds multiple beans of the same type, it refuses to guess which one we intended. Unlike some frameworks, Spring does not inject one arbitrarily.
How would you fix it?
Option 1 — Use @Primary
@Service
@Primary
class StripePaymentService implements PaymentService {
}Spring now injects StripePaymentService by default.
Option 2 — Use @Qualifier
@Autowired
@Qualifier("paypalPaymentService")
private PaymentService paymentService;Now the desired implementation is explicit.
Option 3 — Inject all implementations
Sometimes we actually want every implementation.
@Autowired
private List<PaymentService> paymentServices;Spring injects:
[
StripePaymentService,
PaypalPaymentService
]This is commonly used for:
Notification channels
Authentication strategies
Rule engines
Plugin architectures




