Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

What happens when you inject List<Validator> or Map<String, Validator> in Spring, and how is the list order decided?

When we inject List<Validator>, Spring injects every bean of type Validator, and for Map<String, Validator> it injects all of them keyed by bean name. The list is sorted by @Order, the Ordered interface or @Priority, with lower values first; beans without any order come last, in registration order. If there may be no Validator beans at all, a required List fails at startup, so we use ObjectProvider<Validator>.orderedStream() instead.

Why this question is asked

Injecting a collection of beans is how Spring applications build plug-in style designs: validators, filters, event handlers, payment providers. It lets us add behaviour by adding a bean, without touching the code that uses them. Interviewers ask this question because the details, the map keys, the ordering rules and the empty case, are exactly where real bugs come from. In this answer, we will recap collection injection, see what a List and a Map receive, walk through how the order is decided, and handle the case where no beans exist.

1A Quick Recap of Collection Injection

Normally an injection point of type Validator needs exactly one matching bean. When the injection point is a collection type, List<Validator>, Set<Validator>, Map<String, Validator> or an array Validator[], Spring treats it differently: it collects every bean that matches the element type and injects them together. There is no ambiguity, because we asked for all of them, so @Primary does not reduce the list.

2What a List and a Map Receive

Here are three validators, one of them without an order, and a service that takes both shapes.

Java
@Component @Order(2) class FormatValidator implements Validator { }
@Component @Order(1) class NotEmptyValidator implements Validator { }
@Component class BusinessValidator implements Validator { } // no order
@Service
class SignupService {
SignupService(List<Validator> list, Map<String, Validator> map) {
System.out.println(list); // [NotEmptyValidator, FormatValidator, BusinessValidator]
System.out.println(map.keySet()); // [businessValidator, formatValidator, notEmptyValidator] (order not guaranteed)
}
}

Notice that the Map keys are bean names: the class name with a lowercase first letter for scanned components, or the method name for @Bean methods. A Map is useful when the code chooses a bean by name at runtime, for example from a configuration value. The List is sorted by the order annotations; the Map is a lookup structure, so we should not rely on its iteration order.

3How the List Order Is Decided

Spring sorts the list with AnnotationAwareOrderComparator. For each bean it finds an order value: from the Ordered or PriorityOrdered interface if the bean implements it, otherwise from @Order or @Priority on the class, or @Order on the @Bean method that created it. Lower values come first, so @Order(1) runs before @Order(2), and Ordered.HIGHEST_PRECEDENCE, which is Integer.MIN_VALUE, comes first of all.

A bean with no order at all is treated as Ordered.LOWEST_PRECEDENCE, which is Integer.MAX_VALUE, so it sorts after every ordered bean. Because the sort is stable, beans with equal order values, including all unordered beans, keep the order in which they were registered. Registration order depends on scanning and configuration order, which can change when packages or classes are renamed, so if order matters for a bean, we should give it an explicit @Order rather than rely on where it happens to be registered.

How SignupService's list is ordered
BeanOrder valuePosition
notEmptyValidator@Order(1)1st
formatValidator@Order(2)2nd
businessValidatornone -> Integer.MAX_VALUE3rd, then by registration
Lower values first; unordered beans last.

4@Order Versus Ordered: A Detail Worth Knowing

@Order affects the position in an injected collection, and also the order of things like filters and listeners, but it does not change the order in which singletons are created. A bean with @Order(1) is not necessarily created before one with @Order(2). If one bean must exist before another, that should be expressed as a dependency, or @DependsOn in rare cases.

Ordered as an interface is useful when the order is computed, for example read from configuration; @Order is simpler when it is fixed. When both are present, the interface wins.

Java
@Component
class ConfigurableValidator implements Validator, Ordered {
private final int order;
ConfigurableValidator(@Value("${validators.configurable.order:50}") int order) { this.order = order; }
@Override public int getOrder() { return order; } // 50 unless configured
}

5When There May Be No Beans

A collection injection is still a required dependency. If no Validator bean exists, Spring fails at startup rather than injecting an empty list.

Java
SignupService(List<Validator> validators) { }
// No Validator beans:
// Parameter 0 of constructor in SignupService required a bean of type 'java.util.List' that could not be found.
SignupService(ObjectProvider<Validator> validators) {
this.validators = validators;
}
void signup(Form f) {
validators.orderedStream().forEach(v -> v.validate(f)); // empty stream if none: nothing runs
}
Step throughWhat Spring does for List<Validator>
  1. Find every bean whose type is Validatorincludes beans from parent contexts
  2. Any found?
    YesContinueNoRequired: startup fails; ObjectProvider: empty stream
  3. Get each bean's order valueOrdered interface, then @Order or @Priority
  4. Stable sort, lowest value firstunordered beans last, in registration order
  5. Inject the sorted list

5 steps in order. Play them, or step through them one at a time.

ObjectProvider.orderedStream() applies the same ordering rules as the List but returns an empty stream when there are no beans, and it looks the beans up at call time. Other options are @Autowired(required = false) on the field or setter, or an Optional<List<Validator>> parameter, but ObjectProvider is the clearest for this case, which is common in libraries that let applications plug in zero or more beans.

6Follow-up questions

Does @Primary affect a List injection?

No. @Primary only breaks ties when a single bean is wanted. A List or Map injection receives every candidate, including the primary one and all the others, and the primary bean is not moved to the front either.

Can a bean be excluded from collection injection?

Yes. Marking a bean definition autowireCandidate = false, for example @Bean(autowireCandidate = false), removes it from all autowiring, including collections. Spring Framework 6.2 also added defaultCandidate = false, which keeps a bean out of plain injection points unless they use a qualifier (check your version for exactly how it applies to collections). A qualifier on the List injection point, such as @Qualifier("strict") List<Validator>, includes only beans with that qualifier.

What happens with generic types, such as List<Validator<Order>>?

Spring matches generics, so List<Validator<Order>> receives only validators declared for Order, not Validator<Payment>. That is useful for typed plug-ins, and also a common reason a bean seems to be missing from a list: its declared generic type does not match.

7Conclusion

In this answer, we saw that injecting List<Validator> gives every Validator bean, and Map<String, Validator> gives them all keyed by bean name. The List is sorted with AnnotationAwareOrderComparator: values from the Ordered interface, @Order or @Priority, lowest first, with unordered beans last in registration order, and we should not rely on a Map's iteration order. A collection injection is still required, so when there may be no beans at all, ObjectProvider<Validator>.orderedStream() gives the same order and an empty stream instead of a startup failure.

What to say in the interviewAbout 90 seconds aloud

Open with

When I inject List of Validator, Spring does not look for one bean; it collects every bean whose type matches Validator and injects them all, so @Primary has no effect there.

Then, if they want more

With Map of String to Validator I get the same beans keyed by bean name, which is the class name with a lowercase first letter for scanned components or the method name for @Bean methods. That is handy when the code chooses a validator by name at runtime, but I would not rely on the map's iteration order.

The List order is decided by AnnotationAwareOrderComparator. Each bean's order value comes from the Ordered interface if it implements it, otherwise from @Order or @Priority on the class, or @Order on the @Bean method. Lower values come first.

Beans with no order get the lowest precedence, Integer.MAX_VALUE, so they go last, and because the sort is stable they keep their registration order among themselves. Since registration order can change with package or class renames, if order matters I always set it explicitly. Note that @Order controls position in the collection, not the creation order of beans.

One more trap: a List injection is still required, so if there are no Validator beans at all, startup fails. If zero is a valid case, I inject ObjectProvider of Validator and use orderedStream, which applies the same ordering and simply returns an empty stream.

About this answer

Last updated
12 November 2026
Versions it names
Spring Framework 6
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "What happens when you inject List<Validator> or Map<String, Validator> in Spring, and...": https://scrivio-interview-library.netlify.app/spring-boot/dependency-injection/96e80b3db0/