Introduction to Spring (Factory Bean)

Kasun Dissanayake
4 min readMar 25, 2019

--

In this tutorial we’ll understand what is Spring Factory and some fundamental concepts of Spring. The first thing we should consider is Dependency Injection when creating a Spring Application. The whole Dependency Injection concept is possible because Spring is actually a Container of Beans. Spring behaves as a Factory of Beans.

What is Container ?

Let’s take an example of Servlet Container to explain Container. Tomcat is a servlet container. What that means is, Tomcat creates the Servlet objects which we require in order to run an application.

You would configure all the servlets in XML and supply the classes. Then Tomcat reads the XML and it understands what are the servlets that need to be initiated. Then it creates these Servlets.

Spring is something similar to that. Spring is a container but not a container of Servlets. It is a container of Beans. Inside a Spring Container you can have many number of Objects that you want. And all those objects are managed by the Spring Container. Actually Manage means, It handles the instantiation of all the objects, the whole life cycle of the Objects and handles the whole destruction of the Objects.

You can have objects outside the container as well as in your application. But adding the objects inside the container helps Spring to manage Objects easily. It gives you lot of advantages.

The point here is that we are learning and implementing Spring in order to manage Object Life Cycle. To mange Object Life Cycle Spring needs to know what objects exists and where they are. Actually Spring need to own these Objects.

The creation of the Objects is actually done by Spring. If we have a Object called “A” outside the container and it depends on an inside Object called “B”. Normally you would say call Object A and call Object B and inside the Object A you would say Object B = new Object(). It is creating a new Object inside Object A. If I do that the new Object that I created is not handled by the Spring Container because we created that outside the container. Spring doesn’t know that the Object A is exists. Instead of writing code with a new keyword you can write the code to ask Spring Container to initiate this object B and pass it to Object A.

This is something called Factory Pattern. Let’s talk about what Factory Pattern is.

I have Object A and instead of creating a new Object B, I can ask a call to create another Object which is known as Factory Object. Factory Object creates(produces) new Objects for any call. So that I can create Objects using Factory Object and pass those Objects to calling Object (Object A).

How does the Factory Object produce Objects ?

Actually Factory Object reads from a Configuration. Factory Object probably has a Configuration file somewhere or data which has details about all the Objects that needs to create. The Object A will tell “I need a new Object of Object B” to the Object Factory. Then The Object Factory finds out what is the blueprint of that particular Object specification from the Configuration File . Then Object Factory knows what Object needs to be created. So it is going to create a new Object and it will handover the requesting Object to Object A. Now Object A has the Object it wants. We can use Spring library in order to do this functionality. Here Spring has a Bean Factory. We can use Spring Bean Factory to create Objects for us.

I have an Object A and I would call Bean Factory to create a new Object. Bean Factory would read from a Spring XML(Spring XML contains all the Beans definitions and blueprints). Bean Factory creates a Bean from this Blue Print and it is called as Spring Bean. Newly created Spring Bean would hand over to Requested Object.

The Advantage here is that since the new Bean has been created in the Bean Factory spring knows about it. Spring manages entire life cycle of Bean. In this case Spring acts as a container for the newly created Bean. This is a high level overview of how can we use Spring Bean Factory in order to create Beans.

So we will have a main method in Object A which calls a Spring Bean Factory and then we will have to provide a Spring XML as well (We define our Bean in Spring XML).

We will talk to the Bean Factory and say “Hey Bean Factory get me that Bean form reading Spring XML”.

Today we learnt about basic factory functionality of Spring Framework.

Thank You !!

--

--

Kasun Dissanayake
Kasun Dissanayake

Written by Kasun Dissanayake

Senior Software Engineer at IFS R & D International || Former Software Engineer at Pearson Lanka || Former Associate Software Engineer at hSenid Mobile

No responses yet