1. Introduction
In the Spring Framework, one of the core ideas is Inversion of Control (IoC), also known as Dependency Injection (DI). It means that instead of manually creating and managing object dependencies in your code, Spring handles it for you through a container.
The container is responsible for:
- Creating and managing objects (known as beans)
- Injecting dependencies between them
- Managing their lifecycle
Spring provides two main IoC containers to manage beans:
- BeanFactory
- ApplicationContext
Although both are used to manage beans, there are important differences between them in terms of initialization, functionality, and features.
2. BeanFactory Overview
2.1 Definition
The BeanFactory is the simplest container provided by the Spring Framework.
It is the root interface for accessing the Spring bean container and provides basic dependency injection support.
It is defined in the package:
org.springframework.beans.factory.BeanFactory
2.2 Characteristics
- Lazy Initialization:
Beans are created only when requested (on demand).
This saves memory and startup time but may increase the response time when a bean is first needed. - Lightweight:
BeanFactory consumes fewer resources and is suitable for memory-constrained environments such as mobile or embedded systems. - Basic Functionality Only:
It provides only core features like bean creation, wiring, and dependency injection.
It does not support advanced features such as event publishing or internationalization.
2.3 Example
Step 1: Create a simple bean class:
public class HelloWorld {
private String message;
public void setMessage(String message) {
this.message = message;
}
public void getMessage() {
System.out.println("Message: " + message);
}
}
Step 2: Define the bean in beans.xml:
<beans>
<bean id="helloWorld" class="com.example.HelloWorld">
<property name="message" value="Hello from BeanFactory!" />
</bean>
</beans>
Step 3: Load the bean using BeanFactory:
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
public class MainApp {
public static void main(String[] args) {
BeanFactory factory = new XmlBeanFactory(new ClassPathResource("beans.xml"));
HelloWorld obj = (HelloWorld) factory.getBean("helloWorld");
obj.getMessage();
}
}
Output:
Message: Hello from BeanFactory!
3. ApplicationContext Overview
3.1 Definition
The ApplicationContext is a more advanced container that builds upon BeanFactory.
It is defined in the package:
org.springframework.context.ApplicationContext
It not only manages beans but also provides:
- Event publishing
- Message resource handling (i18n)
- Annotation-based configuration
- AOP integration
- Bean post-processing and lifecycle callbacks
3.2 Characteristics
- Eager Initialization:
By default, all singleton beans are created at container startup, ensuring faster access at runtime. - Enhanced Functionality:
It supports enterprise-level features like:- Application events
- Declarative transactions
- Internationalization (i18n)
- Automatic bean post-processing
- Context Hierarchy:
You can create multiple contexts in a hierarchy (e.g., parent-child contexts in web applications).
3.3 Example
Step 1: Create the same bean HelloWorld.java as before.
Step 2: Define the bean in beans.xml:
<beans>
<bean id="helloWorld" class="com.example.HelloWorld">
<property name="message" value="Hello from ApplicationContext!" />
</bean>
</beans>
Step 3: Load the bean using ApplicationContext:
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
obj.getMessage();
}
}
Output:
Message: Hello from ApplicationContext!
4. Key Differences Between BeanFactory and ApplicationContext
| Feature | BeanFactory | ApplicationContext |
|---|---|---|
| Initialization Type | Lazy (beans created when requested) | Eager (beans created at startup) |
| Memory Usage | Lightweight | Slightly heavier |
| Configuration Source | XML-based | XML, Annotation, Java Config |
| Event Handling | Not supported | Supported (e.g., ContextRefreshedEvent) |
| Internationalization (i18n) | Not supported | Supported |
| AOP Integration | Limited | Fully supported |
| Autowiring and Bean Post Processors | Manual | Automatic |
| Lifecycle Callbacks | Minimal | Fully supported |
| Use Case | Suitable for simple standalone applications | Suitable for enterprise or web applications |
| Implementation Classes | XmlBeanFactory (deprecated) | ClassPathXmlApplicationContext, AnnotationConfigApplicationContext, FileSystemXmlApplicationContext |
5. Detailed Explanation of Functional Differences
5.1 Eager vs Lazy Initialization
- BeanFactory:
Loads bean definitions but does not create instances until requested. - ApplicationContext:
Instantiates all singleton beans as soon as the context is initialized, improving runtime performance.
5.2 Event Publishing
- ApplicationContext can publish and listen to events using ApplicationEventPublisher and ApplicationListener.
- BeanFactory does not support event handling.
Example:
context.publishEvent(new MyCustomEvent(this, "App Started"));
5.3 Internationalization (i18n)
- ApplicationContext supports message resource bundles for multilingual applications using:
context.getMessage("welcome.message", null, "Default Message", Locale.US); - BeanFactory lacks this feature.
5.4 Integration with AOP and Web Modules
- ApplicationContext integrates seamlessly with Spring AOP, Spring MVC, and Spring Boot.
- BeanFactory is mainly used for core dependency injection and lacks integration with higher-level modules.
6. When to Use BeanFactory vs ApplicationContext
6.1 Use BeanFactory When:
- You need a lightweight container with limited memory (e.g., IoT or mobile devices).
- Your application is simple and doesn’t require advanced features like AOP or event handling.
- You want to delay bean creation until absolutely necessary.
6.2 Use ApplicationContext When:
- You are building enterprise-level applications.
- You need advanced container features like events, internationalization, or annotation-based configuration.
- You prefer faster access to beans after startup.
- You are using Spring Boot or Spring MVC, where ApplicationContext is the standard.
7. Performance and Resource Considerations
| Aspect | BeanFactory | ApplicationContext |
|---|---|---|
| Startup Time | Faster (lazy loading) | Slightly slower (eager loading) |
| First Access Time | Slower (creates bean on first call) | Faster (bean already available) |
| Memory Consumption | Low | Moderate |
| Best Use Case | Lightweight, resource-limited apps | Enterprise and web apps |
8. Deprecation Note
XmlBeanFactory was deprecated in Spring 3.1 and replaced by:
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
This means that ApplicationContext is the recommended container for all modern Spring applications.
9. Summary
- BeanFactory is the basic IoC container — it is lightweight and performs lazy initialization.
- ApplicationContext is a superset of BeanFactory — it provides enterprise-level features such as event propagation, i18n, and annotation support.
- In most modern Spring projects (especially Spring Boot), you will always use ApplicationContext.
