Spring IoC Container

The core concept of Spring framework is its Inversion of Control.IoC container has following responsibility.

  • Object creation
  • Dependency Satisfaction
  • Object Life cycle management

IoC is also known as dependency injection (DI) which deals with the dependencies of an object.The dependencies of an object can be provided through constructor argument,factory method or by setter method.We can provide the information to IoC container through XML or Annotation. The container then injects those dependencies when it creates the bean.

Terminology of IoC

There are following terms need to understand to use Ioc Container.


It is a POJO class whose object is created and managed by IoC container.


It is an interface which defines the functionalities of basic Ioc container.Most commonly used implementation of this interface is XmlBeanFactory.It receives bean configuration by XML.Most commonly used methods of this interface is getBean().


It is an interface which defines the functionalities of advanced Ioc container.Most commonly used implementation of this interface is ClassPathXmlApplicationContext .It receives bean configuration by XML.


It is an interface which contains the methods to manage beans configuration.Most commonly used implementation class of this interface are.

  • ClassPathResource
  • FileSystemResource

Steps for Creating bean using Ioc container

  • Add jar file of spring core module.
  • Define POJO class with default constructor,getter and setter methods.
  • XML configuration is created to provide beans configuaration.
  • Resource object is created to provide configuration of beans to IoC container.
  • A BeanFactory object is obtained and reference of Resource object is provided to it.
  • Now getBean() is used to create object

Now take an example to do the same.

POJO class

package com.startwithjava.beans;

public class Customer {
	private String name;
	private String address;
	private String zipcode;
	public Customer() {
	public Customer(String name, String address, String zipcode) {
		this.name = name;
		this.address = address;
		this.zipcode = zipcode;
	public String getName() {
		return name;
	public void setName(String name) {
		this.name = name;
	public String getAddress() {
		return address;
	public void setAddress(String address) {
		this.address = address;
	public String getZipcode() {
		return zipcode;
	public void setZipcode(String zipcode) {
		this.zipcode = zipcode;
	public void display(){
		 System.out.println("Name:-"+name+"\nAddress:-"+address+"\nZip Code:-"+zipcode);  


XML Configuration

The file applicationContext.xml contains information regarding beans configuration.Here we are using setter injection.

<?xml version="1.0" encoding="UTF-8"?>  
<bean id="customerbean" class="com.startwithjava.beans.Customer">  
<property name="name" value="Raj Kumar"></property>  
<property name="address" value="New Delhi"></property>  
<property name="zipcode" value="22"></property>  


package com.startwithjava;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import com.startwithjava.beans.Customer;
public class TestIOC {

	  public static void main(String[] args) {  
		  Resource resource=new ClassPathResource("applicationContext.xml");  
		    BeanFactory factory=new XmlBeanFactory(resource);  
                    //customerbean bean id define in XML	      
		    Customer c=(Customer)factory.getBean("customerbean");  

Download Source Code

Leave a Reply