Spring Framework Series — IOC Container

devcat
3 min readOct 8, 2023

--

Spring framework is a framework which makes it easy to build enterprise java application. This is a series of blog post which will explain about the features of spring ecosystem. Spring provides support for dependency injection, validation, resolving messages, data access ..etc.

In this blog post, we will understand what is IOC Container?. If you working in spring framework or if you find it difficult to understand how and why spring framework do certain things, Then this article is for you.

Let’s first look at what is IOC Container?.

IOC Container is a container which uses IOC (Inversion of Control) mechanism to create, assemble and manage beans in Spring. Why we need container to do this when objects can be created, assembled and managed by the developer. Spring IOC Container takes care of this plumbing work to help developers to focus on writing business logic. So what is bean in spring?. To simply say that bean is nothing but an object in spring. When we create any bean such as class with dependencies, Spring will take care of creating an object with all the dependent objects. If the dependent objects do not exists in the container, spring will create and register them in container, otherwise it will directly load them from the container.

If we are going to create spring based java application, then we need to let the spring know from where it has to load the bean definitions. Whether it has to load them from XML or whether it has to load them from classpath. So, How spring should load the beans from configuration metadata. There are multiple ways that we could create configuration metadata. Mostly spring objects are configured in xml, annotation or java. If we have to load and create container based on that, we have to tell the spring where it can find.

As specified in the above diagram, spring IOC container is created from the xml, annotation or java based configuration. In the following examples, we will see an example of how to create ApplicationContext(IOC Container) to create and manage beans in spring.

ApplicationContext is an interface in spring, It extends BeanFactory to add some extra capabilities. AnnotationConfigApplicationContext, ClassPathXmlApplicationContext, GenericApplicationContext..etc are some of the classes implement ApplicationContext interface.

// Person bean 
public class Person {
private Integer id;
private String name;
private Integer age;
private String dept;

public Person(Integer id, String name, Integer age, String dept) {
this.id = id;
this.name = name;
this.age = age;
this.dept = dept;
}

public void init() {
System.out.println("Post Construction of object Person");
}

public void destroy() {
System.out.println("Pre Destroy of object Person");
}

}
<?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/
https://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="person" class="com.example.spring.core.Person" init-method="init" destroy-method="destroy" scope="singleton">
<constructor-arg name="id" type="Integer" value="1"/>
<constructor-arg name="name" type="String" value="Vandersey"/>
<constructor-arg name="age" type="Integer" value="34"/>
<constructor-arg name="dept" type="String" value="Tax"/>
</bean>
</beans>
// loading bean definitions from xml
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

Person person = context.getBean("person");

// loading and registering beans manually with AnnotationConfigApplicationContext
AnnotationConfigApplicationContext context1 = new AnnotationConfigApplicationContext();
context1.registerBean("person", Person.class, 1, "name", 35, "ML Dept");

//or
// registering beans with scan

context1.scan("com.example.spring.core");
context1.refresh();

Person person = context1.getBean("person");
// java configuration

@Configuration
public class JavaContainerConfig {

@Bean
public Person person() {
Person person = new Person();
person.setId(2);
person.setName("name2");
person.setAge(21);
person.setDept("MK Dept");
return person;
}

}
// loading and registering beans manually with java @Bean configuration
AnnotationConfigApplicationContext context2 = new AnnotationConfigApplicationContext();

context2.registerBean(JavaContainerConfig.class);

context2.refresh();

Person person = context2.getBean("person");

In the above examples, we have Person bean which is loaded into the container from xml, In another snippet, we used AnnotationConfigApplicationContext to load and register beans directly instead of loading from xml. In the third approach, we used java configuration to create Person bean which is automatically registered when JavaContainerConfig class is registered. All of the above approaches do one thing, create a spring container from configuration.

Conclusion

We have seen how to create Spring IOC container which manages the bean (object) lifecyle of spring based java application. We have also seen how to create a container from xml, annotation or java confgiuration. What we have learned from this post, Spring IOC container is a dependency injection container which manages lifecycle of all the objects in spring application. We have learned how to work with IOC container using different configuration.

--

--