1. Introduction
This question is one of the common Spring interview questions when applying for Java/Spring developer position. In this tutorial, we'll see what these two actually are and what are differences between Bean Factory and Application Context in Spring.
First, let's discuss a Spring Bean, to easier understand the concept of Bean Factory and Application Context in Spring.
Spring Bean represents a Java object managed by the Spring Container. This means that the Spring Container takes care of instantiating, configuring, and assembling of Spring Beans.
2. What Are Differences Between Bean Factory and Application Context in Spring?
Let's see what are the main differences between the Bean Factory and Application Context:
- Bean Factory is the basic interface for interaction with Spring container while the Application Context implements that interface and adds much more features that make Spring capable to support big enterprise application
- Bean Factory uses lazy loading - this means that beans are instantiated on-demand, while Application Context uses eager loading - beans are instantiated right after the ApplicationContext is initialized. This makes Bean Factory much more memory friendly and it's usually recommended to use it when the memory consumption is a critical factor in the application
- Application Context has the following enterprise features:
- Bean instantiation and wiring
- ApplicationEvent publication
- Convenient MessageSource access (i18n)
- Application Context automatically registers BeanFactoryPostProcessors and BeanPostProcessor while with Bean Factory they need to be executed manually
3. Conclusion
In this short tutorial, we showed what are the main differences between the Application Context and Bean Factory in Spring. For more tutorials on Spring, check out this page.