How does Spring IoC Container create objects for us?

Sagar Kudu
3 min readMar 29, 2021

Spring container is a predefined program or we can say that it is a component of spring that we get from the Spring framework.

Official — The IoC container

Spring IoC container is mainly responsible for creating the object, holding objects into memory, and injecting them into another object as required.

  1. responsible for the creation of objects

2. if objects are created then they will hold in memory.

3. If required it can inject an object into another object, which means it can do the work of Dependency Injection.

→ it means it maintains the lifecycle of any object i.e so complete lifecycle from creation to destruction is maintained by Spring IoC container, these all stages care is taken by Spring Container we don’t need to do anything, just have to specify two things to the Spring viz Beans and Configuration file.

  1. Tell which Beans or POJO classes spring have to manage, so will tell all the configuration of beans.

2. We will tell the Configuration file (say XML configuration file).

→ In these, we will specify which ‘Bean is dependent on which things’ and spring container will make use of these configuration files. In this, we have specified which object is dependent on which object, so it creates the object of that bean and as per the requirement as we have specified it will inject the dependency so that we can use it.

→ Once the injection is done then the next part will be of our Application (Main.java).

ApplicationContext

It represents a Spring IoC container, which means it is an interface that is going to help us to inject the dependencies.

→ Instead of ApplicationContext we also have BeanFactory but we have all the features of BeanFactory already. So ApplicationContext basically extends BeanFactory, so it has all properties of BeanFactory in addition it also has its own properties. So it represents a Spring IOC container.

ApplicationContext is an interface so we will make objects so that we can get values from the container and this container will have objects and we get these values with the help of ApplicationContext but the problem is as ApplicationContext is an interface we can’t create its objects, so in these case, we have created an object of its subclass.

Subclasses for ApplicationContext

  1. ClassPathXMLApplicationContext (widely used)
  2. AnnotationConfigApplicationContext
  3. FileSystemXMLApplicationContext

ClassPathXMLApplicationContext

→ it searches for XML configuration from java classpath, so if we want to keep XML configuration in the classpath and inside that we want to define a configuration so will have to use ClassPathXMLApplicationContext, so it can scans or searches for XML configuration in java class path.

AnnotationConfigApplicationContext

→ It searches for that beans on which added an Annotation

FileSystemXMLApplicationContext

→ It basically searches for config files from the file system.

Check out my latest articles and please show some love if you like an article.❤

For queries reach out to me at sagar978@gmail.com.

--

--

Sagar Kudu

I am Full Stack Java Developer @ Tata Strive | Get blogs and tutorials related to the (React | Kafka | DevOps) | Connect https://www.linkedin.com/in/sagarkudu/