Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




Spring Bean Factory / ApplicationContext

In order to understand Spring Bean Factory we need to know a little about Factory Pattern.


Factory Pattern

Let us take the example of a vehicle class to explain Factory Pattern.



Spring_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 Bean Factory / ApplicationContext

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:


Spring_Bean_Factory

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.


How does Spring know about the Beans it needs to create?

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' :


class Car {

 String brand;

 public String getBrand() {
   return brand;
 }

 public void setBrand(String brand) {
   this.brand = brand;
 }
}

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'.


NOTE : You can give any name to 'application-context.xml'.

What does application-context.xml contain?

application-context.xml

<?xml version = "1.0" encoding = "UTF-8"?>

<beans xmlns = "http://www.springframework.org/schema/beans"
 xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation = "http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
 <bean name = "car" class = "Car">
   <property name = "brand" value = "Hyundai"/>
 </bean>
</beans>

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.


<bean name = "car" class = "Car">
   <property name = "brand" value = "Hyundai"/>
</bean>

Just compare the above code with the 'Car' class.

You have a 'Car' class and in the XML we have linked them using :


<bean name = "car" class = "Car">

Also your 'Car' class has a property named 'brand'. And we are going to initialize the 'brand' property with the value 'Hyundai'.


<property name = "brand" value = "Hyundai"/>

How does Bean Factory / ApplicationContext interact with the XML file(application-context.xml)?

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:


Example with ApplicationContext

import org.springframework.context.ApplicationContext;

public class FirstSpringApp {
   public static void main(String[] args) {

   ApplicationContext applicationContext = new ClassPathXmlApplicationContext("application-context.xml");

   Car car = (Car)applicationContext.getBean("car");

   System.out.println("The Car brand is : "+car.getBrand());

   }
}

1. Firstly, our Application has requested a 'Car' object from 'ApplicationContext'


Car car = (Car)applicationContext.getBean("car");

2. So, the 'ApplicationContext' takes the help of 'application-context.xml' to get the list of beans.


ApplicationContext applicationContext = new ClassPathXmlApplicationContext("application-context.xml");

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.


System.out.println("The Car brand is : "+car.getBrand());

Example with BeanFactory

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;

public class FirstSpringApp {
   public static void main(String[] args) {

   BeanFactory beanFactory=new XmlBeanFactory(new ClassPathResource("application-context.xml"));

   Car car = (Car)applicationContext.getBean("car");

   System.out.println("The Car brand is : "+car.getBrand());

   }
}

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.


BeanFactory beanFactory=new XmlBeanFactory(new ClassPathResource("application-context.xml"));

NOTE : ApplicationContext is preferred over BeanFactory as it comes with some additional features along with the features of BeanFactory.