In order to understand Spring Bean Factory we need to know a little about Factory Pattern.
Let us take the example of a vehicle class to explain Factory Pattern.
Say you have a Vehicle showroom. Which sells Car, Jeep, Bike etc. The showroom works in the following way. When a customer comes to buy a specific vehicle (Car, jeep etc), you send the order to the 'Vehicle Production Factory' where that specific vehicle is made with the help of some raw materials, which is taken from the internal storage of the factory. Once the Vehicle is made by the 'Vehicle Production Factory', its handed over to the 'Vehicle Showroom'.
So, the 'Vehicle Showroom' is not making the Vehicle but sends the order to the 'Vehicle Production Factory' Where the Vehicle is produced and handed over to the 'Vehicle Showroom'.
Now, if you consider the 'Vehicle' as a class, you will be able to understand that the 'Vehicle' class is not creating an object by itself. Rather it is taking the help of some 'Factory' which is creating an object for the Vehicle class by taking the help of so called 'Internal storage of the factory' which can be an XML file. This is Factory Pattern. Now we will take the help of Factory Pattern in order to understand Spring Bean Factory .
Spring follows Factory Pattern to handle the beans. And as we have seen in factory pattern that, instead of creating a new object yourself, you ask a factory(Spring Bean Factory in this case) to create the object for you.
Let us illustrate with the below diagram:
1. From your application, when you request Spring to create a new object for you. Spring takes the help of Spring Bean Factory or Application Context.
2. Then, Spring Bean Factory/Application Context takes the help of an XML file(Say application-context.xml), which contains the definition of all the beans that needs to be created.
3. After Bean Factory/Application Context gets the bean definition from application-context.xml, it creates a bean.
4. And hands it over to your Application.
The bean can be considered an equivalent of a java class. Creating a bean is same as creating an object using 'new' keyword in java.
If you want spring to create objects for you, you need to inform spring about all the objects that spring has to create for you.
Say there is a class called 'Car' :
And you want spring to create a 'Car' object for you. So, you have to provide the details of the 'Car' class in the XML file 'application-context.xml'.
The xml file is all about beans. In the <bean> tag you can find namespaces xmlns, xmlns:xsi etc. You don't have to bother much about the namespaces and just remember the fact that these namespaces are just like the 'import' statement in java.
They have the definition of the <bean> tag.
Just compare the above code with the 'Car' class.
You have a 'Car' class and in the XML we have linked them using :
Also your 'Car' class has a property named 'brand'. And we are going to initialize the 'brand' property with the value 'Hyundai'.
Now, let us join the dots. i.e. We will see how our Application interacts with BEAN FACTORY or ApplicationContext and application-context.xml.
Below is our Application which is going to talk to BEAN FACTORY / ApplicationContext:
1. Firstly, our Application has requested a 'Car' object from 'ApplicationContext'
2. So, the 'ApplicationContext' takes the help of 'application-context.xml' to get the list of beans.
It says, the 'ApplicationContext' has to look for the 'application-context.xml' in the classpath.
3. Then the 'Car car' object is populated with the values from 'application-context.xml' and finally the 'brand' value is displayed.
In the above case we are creating a 'ClassPathResource' instance and passing 'application-context.xml' in it. Then we are passing it as a parameter to 'XmlBeanFactory' and finally 'BeanFactory' is initialized.