How does Spring framework autowire a collection
Spring's BeanFactory is basically a registry of beans. These beans can be declared using XML, or using @Bean
-annotated methods in a Configuration class, or be automatically discovered using package scanning.
When you ask for a List<Foo>
, Spring finds all the beans that are of type Foo, creates a List containing those beans, and injects that list.
The documentation about Autowired explains it, BTW:
It is also possible to provide all beans of a particular type from the ApplicationContext by adding the annotation to a field or method that expects an array of that type