Spring IoC Container (original) (raw)

Last Updated : 24 Mar, 2026

The Spring Inversion of Control (IoC) container is a core component of the Spring Framework, streamlining object creation and management. It promotes flexibility and maintainability by managing dependencies and configurations automatically, allowing developers to concentrate on core business logic.

The following diagram illustrates how the IoC container manages the creation of beans and injects their dependencies in a Spring application

file

IOC

Types of Spring Containers

1. BeanFactory Container

The BeanFactory is the simplest and most lightweight container in Spring. It provides basic features for bean creation and management but lacks advanced functionalities such as event handling and AOP.

**Example:

Java `

import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.core.io.ClassPathResource;

public class BeanFactoryExample { public static void main(String[] args) { // Load the Spring context (XML configuration) BeanFactory factory = new XmlBeanFactory(new ClassPathResource("beans.xml"));

    // Access the bean from the factory
    MyBean myBean = (MyBean) factory.getBean("myBean");

    // Use the bean
    myBean.doSomething();
}

}

`

**BeanFactory Container does not support annotation-based configuration

2. ApplicationContext Container

The ApplicationContext is a more advanced and feature-rich container compared to BeanFactory. It extends BeanFactory and adds additional features such as event propagation, AOP support, and internationalization.

Syntax for ApplicationContext Ioc Container

1****. Using XML Configuration**

Java `

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

public class ApplicationContextExample { public static void main(String[] args) { // Load the Spring context (XML configuration) ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

    // Access the bean from the application context
    MyBean myBean = (MyBean) context.getBean("myBean");

    // Use the bean
    myBean.doSomething();
}

}

`

**2. Using AnnotationBasedConfiguration

**Step 1. Create Config Class

Java `

import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;

@Configuration public class AppConfig {

@Bean
public MyBean myBean() {
    return new MyBean();
}

}

`

**Step 2. Create MainApp file by using Application Context Ioc Container

Java `

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class AnnotationContextExample { public static void main(String[] args) { // Load the Spring context using annotations AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);

    // Access the bean from the application context
    MyBean myBean = context.getBean(MyBean.class);

    // Use the bean
    myBean.doSomething();
}

}

`

Spring IoC Container Managing Beans and Dependencies through Configuration Metadata

Spring-IOC-Container

How It Work In Spring Application

1. **Configuration Metadata

2. **Business Classes (POJOs)

3. **The Spring Container

4. **Fully Configured System Ready for Use

Why It used in Spring Framework?

BeanFactory vs ApplicationContext

The below table demonstrates the key difference between BeanFactory and ApplicationContext

Feature BeanFactory ApplicationContext
Annotation Support No Yes
Bean Instantiation/Wiring Yes Yes
Internationalization No Yes
Enterprise Services No Yes
ApplicationEvent Publication No Yes
Automatic BeanPostProcessor Registration No Yes
Loading Mechanism Lazy Loading Aggressive Loading
Automatic BeanFactoryPostProcessor Registration No Yes