Spring Bean Factory
In this article we will see how Spring Bean factory works. For understanding the concept lets first understand what is a spring container .
What is spring container?
We know that what is a servlet container,Tomcat is a servlet container. Tomcat creates servlet object which are required in order to run the application you will configure all the servlets in a configuration file and , tomcat reads the XML and identify the servlets to be instantiated and it creates those servlets.
Spring does something similar to this . But not the servlets ,It is a container of beans. So u can have as many objects you want inside the container All these objects are managed by the spring container . Spring container handles instantiating, managing the life cycle of the object, it handles the destruction of the objects.
You can also have objects outside of the container , But adding objects outside the container are not taken care by the container.
In the above figure ObjectA, ObjectB and ObjectC are inside the spring container and ObjectD and ObjectE are outside the container. So if you want to instantiate ObjectD and ObjectE , we need to do new ObjectD() and new ObjectE() respectively.
Spring container will take care of instatiating ObjectA,B and C
Spring Bean Factory
We know that the use of factory pattern, Factory class provide the Object of required class.
lets see the architecture in the below figure
Here when classA required an ObejctB , We will make a call to Object Factory to get the corresponding object. Factory class will take care of instantiating the required class and return the object.
How does Spring been factory provide the bean object?
Spring container reads the configuration file which defines all the objects , this configuration is the blueprint of the beans which are defined in the factory.
When spring container gets request for any bean object defined in spring XML , container will instantiate and provide the object .