The IOC container – Spring Framework

IOC, Spring Framework

The IOC container – Spring Framework

Published on March 7, 2018

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 known as Spring container which is at the core of Spring Framework. The IOC Container is implemented using the 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 the high level view of how Spring works.

The IOC Container uses Business Objects(POJOs) and Configuration metadata to come up with a 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 the 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){
	this.id=id;
  }
  public int getId(){
	return id;
  }

  public void setName(String name){
	this.name=name;
  }
  public int getName(){
	return name;
  }
    
}

Now let’s implement XML based Configuration metadata

<?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.xsd">
 
  <bean id="student" class="Student">
    <property name="id" value="1"/>
    <property name="name" value="Muhammad Aqib"/>
  </bean>
 
 
  <!-- more bean definitions go here -->
 
</beans>

Now access metadata using ApplicationContext Container

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

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());
      
   }
}

Output

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

Recommended insights

Learn more about how we add value!

Start a Project

X

  • WORK WITH US

    Before we start, please fill this form to provide us some basic information.

  • This field is for validation purposes and should be left unchanged.
X

Oops! We could not locate your form.