The IOC Container – Spring Framework

Share on facebook
Share on twitter
Share on linkedin



The IOC Container – Spring Framework

In this article, we’ll discuss the concept of IOC and IOC Container, and then we’ll dive deep into the implementation of IOC container with Spring Framework.

let’s first understand the concept of Inversion of control(IOC). The IOC is a design principle in software engineering. We can easily understand it by Simple Scenario based example described below.

Example: When we are using any framework so the framework is performing most of the tasks for us but there is some particular situation where our custom code needs to be called and perform some custom logic and tasks, The framework is going along with allocating resources and determining when the object or tasks get called so when there is turn for our application specific logic the framework calls in our custom code and our code returns the control back to the framework, this whole flow is known as Inversion Of Control. The image below describes the flow of given example for IOC

let’s now understand the IOC Container with Spring framework. The IOC Container is also know as Spring container which is at the core of Spring Framework. The IOC Container is implemented using Dependency Injection design pattern. IOC Container is responsible to instantiate, configure and assemble the objects as well as manages object’s complete life cycle .

The IOC container gets instructions on what objects to instantiate, configure, and assemble by reading the configuration metadata provided. The configuration metadata can be represented either by XML, Java annotations, or Java code where the objects are known as Beans.

The image shown below describes high level view of how Spring works.

The IOC Container uses Business Objects(POJOs) and Configuration metadata to come up with fully configured system.

There are two types of IOC containers.

BeanFactory and ApplicationContext

The ApplicationContext interface is built on top of the BeanFactory interface. It adds more functionality than BeanFactory such as simple integration with Spring’s AOP, application layer specific context (e.g. WebApplicationContext) for web application. ApplicationContext is generally recommended over BeanFactory.

let’s have some practical example of IOC container.

we’ll implement the concept shown in image above, into the code.

let’s suppose we have Java POJO class Student

public class Student{
  private int id;
  private String name;
  public void setId(int id){;
public int getId(){
return id;
public void setName(String name){;
public int getName(){
return name;

Now let’s implement XML based Configuration metadata

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
       xsi:schemaLocation="  ">
  <bean id="student" class="Student">
    <property name="id" value="1"/>
    <property name="name" value="Muhammad Aqib"/>


Now access metadata using ApplicationContext Container

import org.springframework.context.ApplicationContext;
public class TestApp {
   public static void main(String[] args) {
//let's suppose xml file name is Beans.xml
      ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
      Student student = (Student) context.getBean("student");
      System.out.println("Id: "+student.getId()+" - Name: "+student.getName());


Id: 1 - Name: Muhammad Aqib

Here we have successfully configured our fully working application with the concept of IOC Container.

I hope this short and quick article will help you understand IOC Container concept and it’s implementation with Spring Framework,

If you have any questions or any problem understanding this concept so feel free to ask me or just drop a comment and i’ll help you out regarding the issue,

Best regards,

Muhammad Aqib Arif


Subscribe to our newsletter