@Autowired with Collections in Spring
Fast recall
Quick revision
Core answer
List, Set, or Map, Spring injects all beans of the matching type into that collection. For List, Spring finds every bean implementing PaymentHandler and injects them all. For Map, the keys are the bean names and the values are the bean instances.Whether you know Spring's injection goes beyond single beans and can collect all matching implementations into a List, Set, or Map.
Not knowing this exists and wiring a separate @Autowired field per implementation, or calling ApplicationContext.getBeansOfType() everywhere.
Describe a plugin or Strategy-pattern example — payment processors or notification channels — where you iterate registered handlers, showing you understand open-closed design.
Interview-ready guide
Interview answer
When you put @Autowired on a List or Map field, Spring injects every bean of that type into the collection.
So if you have List of PaymentHandler and three components implementing PaymentHandler, Spring gives you all three in that list. This is useful for strategy patterns where you don't know which implementation you'll need until runtime. Imagine you're building a payment service with a CreditCardHandler, a PayPalHandler, and maybe a CryptoHandler.
Instead of autowiring each one separately, you autowire List of PaymentHandler and loop through to find the one that supports the current payment type. When someone adds an ApplePayHandler later, your service doesn't change at all, because Spring finds the new handler and adds it to the list automatically. For Map injection, the keys are the bean names, so if your class is CreditCardHandler, the key is creditCardHandler.
A common mistake is creating a separate autowired field for each implementation. That couples your code to specific classes. With collection injection, your service only knows about the interface, and Spring wires up all the implementations.
It's the foundation for plugin-style architectures, where you add features without touching existing code. Just remember the order isn't guaranteed, so use @Order if you care about it, and Spring gives you an empty list rather than null when nothing matches.
@Autowired with Collections in Spring
Direct Answer
When @Autowired targets a List, Set, or Map, Spring injects all beans of the matching type into that collection. For List<PaymentHandler>, Spring finds every bean implementing PaymentHandler and injects them all. For Map<String, PaymentHandler>, the keys are the bean names and the values are the bean instances.
Code Example
@Service
public class PaymentService {
private final List<PaymentHandler> handlers;
// Spring injects ALL PaymentHandler beans into the list.
public PaymentService(List<PaymentHandler> handlers) {
this.handlers = handlers;
}
public void processPayment(Order order) {
for (PaymentHandler handler : handlers) {
if (handler.supports(order.getPaymentType())) {
handler.process(order);
return;
}
}
throw new IllegalArgumentException("No handler for " + order.getPaymentType());
}
}
@Component
class CreditCardHandler implements PaymentHandler {
public boolean supports(String type) { return "CREDIT_CARD".equals(type); }
public void process(Order order) { /* Stripe API call */ }
}
@Component
class PayPalHandler implements PaymentHandler {
public boolean supports(String type) { return "PAYPAL".equals(type); }
public void process(Order order) { /* PayPal API call */ }
}
Common Mistakes & Pitfalls
- A separate field per implementation: Wiring
@Autowired CreditCardHandlerand@Autowired PayPalHandlerand branching on each breaks when a new handler appears. Collection injection removes that coupling. - Not understanding Map keys: With
Map<String, PaymentHandler>, keys are Spring bean names (the uncapitalized class name by default). ForCreditCardHandlerthe key is"creditCardHandler", which surprises Learners expecting the class name. - Expecting a specific order: Spring injects into a
List<T>in an undefined order unless you add@Order. Don't rely on position — use asupports()method or explicit priority.
Check your understanding
Answer these without looking back at the article.
- How do you control the order of beans in an @Autowired list?
- What is the Strategy design pattern?
- How does Map<String, Bean> injection work?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.