In this article, we’ll discuss the concept of Inversion of Control (IOC) and IOC Container, and then we’ll dive deep into its implementation with Spring Framework.
Let’s first understand the concept of IOC. It is a design principle in software engineering. We can easily understand it by a simple scenario based on the example described below.
Example: When we are using any framework, it is performing most of the tasks for us. But there are some particular situations 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 a 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 the given scenario:
Let’s now understand the IOC Container with Spring framework. The IOC Container is also known as the 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 manage the 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 gives a 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:
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, and application layer specific context (e.g. WebApplicationContext) for web applications. ApplicationContext is generally recommended over BeanFactory.
Let’s have a look at some practical examples of the 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> </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
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 its implementation with Spring Framework.