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.
- Declare the beans in an bean configuration
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.