I've never seen an autowired collection:
@Service
public class SomeFactory {
@Autowired
private List<Foo> foos;
@PostConstruct
public void init() {
for(Foo foo: foos) {
//do something
}
}
}
In init() method, I can see foos has several entries already. I guess Spring knows who's supposed to be the entry of foos. But, how? What should I do if I want to add a Foo object into foos? Need to configure in a property file, or any other idea?
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
In init() method, I can see foos has several entries already. I guess Spring knows who's supposed to be the entry of foos. But, how?
When the Application Context / Bean Factory is created, by default (if not specify otherwise with lazy init or non singleton scope), all the beans are also created. The app context knows about these beans. So when we try to @Autowire
collections of a particular type beans like private List<Foo> foos;
, Spring find all foos, add them to list and inject into the dependent bean.
What should I do if I want to add a Foo object into foos? Need to configure in a property file, or any other idea?
There are multiple ways to do it. Can choose one suitable option, based on requirement.
XML
file. Flexible. But you may not like doing it in XML.@Component
annotation on a bean class. The class will be scanned by Spring with Component Scan configured. No XML but less flexible. Bean class definition and declaration are tightly coupled. Can not create the @Component
bean in a different Application Context
.@Bean
annotation on a method that returns the bean inside an @Configuration
annotated class. Preferable. Bean class definition and declaration are decoupled and done though annotation only. We can create the @Bean
annotated bean from the method in a different Application Context
as well.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With