Reputation: 15389
I am little confused with these two concepts. Reading the Spring documentation, I found out, for eg. that bean factories are Spring containers. I also read that "ApplicationContext is a complete superset of the BeanFactory". But the difference between the two is not readily apparent. So what is the difference?
Upvotes: 28
Views: 20899
Reputation: 1060
The org.springframework.beans and org.springframework.context packages are the basis for Spring Framework's IoC container. The BeanFactory interface provides an advanced configuration mechanism capable of managing any type of object. ApplicationContext is a sub-interface of BeanFactory. It adds easier integration with Spring's AOP features; message resource handling (for use in internationalization), event publication; and application-layer specific contexts such as the WebApplicationContext for use in web applications.
In short, the BeanFactory provides the configuration framework and basic functionality, and the ApplicationContext adds more enterprise-specific functionality. The ApplicationContext is a complete superset of the BeanFactory, and is used exclusively in this chapter in descriptions of Spring's IoC container.
More details can be found here
Upvotes: 1
Reputation: 41126
Answers from this link attached by Ajinkya is pretty comprehensive, however, I would like to refererence some good points from another material - Spring in Action (Manning Publications):
In a Spring-based application, your application objects will live within the Spring container. As illustrated in figure 2.1, the container will create the objects, wire them together, configure them, and manage their complete lifecycle from cradle to grave (or new to finalize() as the case may be).
There is no single Spring container. Spring comes with several container implementations that can be categorized into two distinct types. Bean factories (defined by the org.springframework.beans.factory.BeanFactory interface) are the simplest of containers, providing basic support for DI. Application contexts (defined by the org.springframework.context.ApplicationContext interface) build on the notion of a bean factory by providing application framework services, such as the ability to resolve textual messages from a properties file and the ability to publish application events to interested event listeners.
On the surface, an ApplicationContext is much the same as a BeanFactory. Both load bean definitions, wire beans together, and dispense beans upon request. But an ApplicationContext offers much more:
- Application contexts provide a means for resolving text messages, including support for internationalization (I18N) of those messages.
- Application contexts provide a generic way to load file resources, such as images.
- Application contexts can publish events to beans that are registered as listeners.
Because of the additional functionality it provides, an ApplicationContext is preferred over a BeanFactory in nearly all applications. The only times you might consider using a BeanFactory are in circumstances where resources are scarce, such as a mobile device.
Aside from the additional functionality offered by application contexts, another big difference between an application context and a bean factory is how singleton beans are loaded. A bean factory lazily loads all beans, deferring bean creation until the getBean() method is called. An application context is a bit smarter and preloads all singleton beans upon context startup. By preloading singleton beans, you ensure that they will be ready to use when needed—your application won’t have to wait for them to be created.
Upvotes: 35