I'm beginner with Spring and I have issues understanding how those two work. I will work my question through example.
We have project with Spring Core module, in that project we have two classes annotated with @Component
in different packages and one main class in third package.
Now we create three different @Configuration
classes and on one we specify @ComponentScan(package with first @Component annotated class)
lets call it config1 and on seconds we specify @ComponentScan(package with second @Component annotated class)
lets call it config2 and on third we specift @ComponentScan(both packages)
lets call it config3.
Now in main class we have following code:
public static void main(String[] args) {
// point 1 in time
System.out.println("lalalalalala");
// point 2 in time
AnnotationConfigApplicationContext context1 =
new AnnotationConfigApplicationContext(config1.class);
// point 3 in time
AnnotationConfigApplicationContext context2 =
new AnnotationConfigApplicationContext(config2.class);
// point 4 in time
AnnotationConfigApplicationContext context3 =
new AnnotationConfigApplicationContext(config3.class);
}
My understanding:
At point 1 in time, we have our application with Spring Core running. At this point Spring created Container and it has no objects. Its empty. Point 1 in time
So then we create object of AnnotationConfigApplicationContext
type. We send config1 class as parameter to this object. Then this object will read @ComponentScan
annotation and scan for POJO's and metadata. After scanning, AnnotationConfigApplicationContext
object will create beans (1 bean in this example, and its eager loading with this type of context) and place them in Spring Container. So at this point in our application we, for the first time, have something in our Spring Container.
point 2 in time
Object of type AnnotationConfigApplicationContext
will only have reference to bean that we put in Container?! So when we call getBean()
method, our Context will "bring" bean from Container?
Same thing happen with point 3 in time, and point 4 in time...
So, basically my question here is: Object that is some kind of implementation of BeanFactory
, READS POJO's and metadata, CREATE BEANS and PLACE them into Spring Container? It does not hold beans, it only points at those beans that are in Container? And Container only contains those beans, he doesn't have any function except holding beans, like its just place in memory? And also we only have one Container in our application, we can have multiple BeanFactory objects but only one Container?