Clean • Professional
Spring Framework uses IoC (Inversion of Control) Containers to manage the creation, configuration, and lifecycle of objects called beans.
These containers remove the responsibility of object management from the application code and handle it centrally.
Spring provides two main types of IoC containers:

BeanFactory is the simplest and most lightweight IoC container in Spring.
org.springframework.beans.factory.BeanFactoryStep 1: Create a Bean Class
public class HelloService {
public void sayHello() {
System.out.println("Hello from BeanFactory");
}
}
Step 2: XML Configuration (beans.xml)
<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="helloService" class="com.example.HelloService"/>
</beans>
Step 3: Use BeanFactory
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class BeanFactoryExample {
public static void main(String[] args) {
BeanFactory factory =
new ClassPathXmlApplicationContext("beans.xml");
HelloService service =
factory.getBean("helloService", HelloService.class);
service.sayHello();
}
}
getBean() is calledOutput
Hello from BeanFactory
Note: In Spring Boot, developers almost never use BeanFactory directly.
ApplicationContext is a powerful and feature-rich IoC container built on top of BeanFactory.
org.springframework.context.ApplicationContextStep 1: Create Bean Class
import org.springframework.stereotype.Component;
@Component
public class HelloService {
public void sayHello() {
System.out.println("Hello from ApplicationContext");
}
}
Step 2: Create Configuration & Main Class
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("com.example")
public class ApplicationContextExample {
public static void main(String[] args) {
ApplicationContext context =
new AnnotationConfigApplicationContext(ApplicationContextExample.class);
HelloService service = context.getBean(HelloService.class);
service.sayHello();
}
}
@ComponentOutput
Hello from ApplicationContext
@SpringBootApplication
public class MySpringBootApp {
public static void main(String[] args) {
ApplicationContext context =
SpringApplication.run(MySpringBootApp.class, args);
HelloService service = context.getBean(HelloService.class);
service.sayHello();
}
}
Important: Every Spring Boot application automatically runs using ApplicationContext.
Both BeanFactory and ApplicationContext are IoC containers in Spring, but they differ in features, behavior, and real-world usage.
| Feature | BeanFactory | ApplicationContext |
|---|---|---|
| Bean Initialization | Uses lazy initialization, meaning beans are created only when they are requested. This saves memory but delays error detection. | Uses eager initialization, meaning all singleton beans are created at application startup, helping catch configuration issues early. |
| Dependency Injection | Supports basic dependency injection only. Suitable for simple object wiring. | Supports advanced dependency injection with annotations, Java config, and enterprise features. |
| AOP Support | Does not provide built-in AOP support. Extra configuration is required. | Provides full AOP support out of the box, making it ideal for cross-cutting concerns like logging and security. |
| Event Handling | Does not support application events such as context refresh or shutdown events. | Supports event publishing and listening, allowing components to communicate in a decoupled way. |
| Internationalization (i18n) | No built-in support for message resolution or localization. | Provides native i18n support, making it easy to build multi-language applications. |
| Enterprise Features | Very limited and minimal by design. | Includes enterprise-level features like lifecycle management, resource loading, and integration with Spring modules. |
| Spring Boot Usage | Rarely used directly in modern applications. | Default and mandatory container used by Spring Boot applications. |
| Real-World Usage | Mostly for learning or legacy systems. | Widely used in production, enterprise, and microservices applications. |
Spring provides two IoC containers:
👉 For modern Spring and Spring Boot applications, ApplicationContext is the recommended and industry-standard choice.