
1. Overview
In this article, we will learn the differences between the ApplicationContext vs BeanFactory.
The Spring framework comes with two IOC containers:
BeanFactoryApplicationContext
The BeanFactory is the root interface for accessing a Spring bean container whereas ApplicationContext extends the BeanFactory and its functionalities.
The Spring IOC container gets its instructions on what bean objects to instantiate, configure, and assemble by reading configuration metadata. We can represent the configuration metadata using any of the below:
- XML configuration
- Annotation-based configuration
- Java-based configuration.
It lets you express the objects that compose your application and the rich interdependencies between those objects. See this bean instantiation article to understand more.
2. Differences between BeanFactory vs ApplicationContext
Let’s see the differences between BeanFactory vs ApplicationContext.
2.1. Bean Initialization
BeanFactory uses lazy initialization but ApplicationContext uses eager initialization. The BeanFactory creates the beans when you invoke or use the beans such as getBeans() method, whereas the ApplicationContext eagerly creates and configures all singleton beans as part of the initialization process.
This pre-instantiation is desirable because you can discover any errors in the configuration or surrounding environment immediately during application start-up, as opposed to runtime hours or even days later.
When this behavior is not desirable, you can prevent pre-instantiation of a singleton bean by marking the bean definition as lazy-initialized. You can refer to this article to learn more about lazy initialization.
2.2. Enterprise specific functionalities
The BeanFactory interface is the root interface that provides the configuration framework and basic functionalities that can manage any type of object. However, the ApplicationContext includes all functionality of the BeanFactory and additionally provides the following enterprise-specific functionalities:
- Easier integration with Spring’s AOP features
- Message resource handling (for internationalization)
- Event publication
- Application-layer specific contexts such as the
WebApplicationContextfor web applications.
We highly recommended using the ApplicationContext in preference to the BeanFactory.
Typically, the ApplicationContext is what you will want to use for most enterprise applications and systems.
2.3. Supported Bean Scopes
The ApplicationContext supports most of the bean scopes, but the BeanFactory only supports two scopes — Singleton and Prototype.
The ApplicationContext provides the application-specific contexts such as the WebApplicationContext in web applications and therefore further “request” and “session” scopes become available in the web environment.
2.4. Dependency Injection
The BeanFactory does not support annotation-based dependency injection whereas the ApplicationContext supports.
2.5. Registration of BeanFactoryPostProcessor and BeanPostProcessor
The ApplicationContext automatically registers BeanFactoryPostProcessor and BeanPostProcessor at startup. However, the BeanFactory does not register these interfaces automatically.
Spring v2.0 and above heavily use the BeanPostProcessor extension point and if you are using just a plain BeanFactory then a fair amount of support such as transactions and AOP will not take effect (at least not without some extra steps on your part) though nothing was actually wrong with the configuration.
2.6. BeanFactory Lightweight
The BeanFactory is lightweight so you might use BeanFactory where memory consumption might be critical and a few extra kilobytes might make a difference.
3. BeanFactory vs ApplicationContext features
| Feature | BeanFactory | ApplicationContext |
|---|---|---|
| Bean instantiation/wiring | Yes | Yes |
| Automatic BeanPostProcessor registration | No | Yes |
| Automatic BeanFactoryPostProcessor registration | No | Yes |
| Convenient MessageSource access (for i18n) | No | Yes |
| ApplicationEvent publication | No | Yes |
4. Conclusion
To sum up, we have learned the differences between the BeanFactory vs ApplicationContext.