Search This Blog

Wednesday 1 February 2017

BeanFactory and ApplicationContext

The org.springframework.beans and org.springframework.context packages are the two most important and fundamental packages in Spring. These packages provides the basis for Spring's Inversion of Control (Dependency Injection) features.
The BeanFactory provides configuration mechanism which is capable of managing beans (objects). The ApplicationContext is build on top of the BeanFactory which adds other functionality such as message resource handling (for use in internationalization), event propagation, easier integration with Springs AOP features, declarative mechanisms.
 The BeanFactory provides the configuration framework and basic functionality, while the ApplicationContext adds enhanced capabilities to it, some of them perhaps more J2EE and enterprise-centric. ApplicationContext is a superset of a BeanFactory, and BeanFactory capabilities and behavior apply to ApplicationContexts as well.
At times we often face the question as whether a BeanFactory or an ApplicationContext are best suited for use in a particular situation. Normally when building most applications in a J2EE-environment, the best option is to use the ApplicationContext, since it offers all the features of the BeanFactory and adds on to it in terms of features, while also allowing a more declarative approach to use of some functionality, which is desirable. In certain cases where memory usage is concerned one might prefer to use BeanFactory over Application Context.

The BeanFactory

The BeanFactory is the container which instantiates, configures, and manages a number of beans. These beans typically collaborate with one another.
A BeanFactory is represented by the  interface  org.springframework.beans.factory.BeanFactory, for which there are multiple implementations. The most commonly used simple BeanFactory implementation is org.springframework.beans.factory.xml.XmlBeanFactory.
The BeanFactory is instantiated as below for different implementation:
Resource res = new FileSystemResource("beans.xml");
XmlBeanFactory factory = new XmlBeanFactory(res);
or
ClassPathResource res = new ClassPathResource("beans.xml");
XmlBeanFactory factory = new XmlBeanFactory(res);
or
ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext(
        new String[] {"applicationContext.xml", "applicationContext-part2.xml"});
BeanFactory factory = (BeanFactory) appContext;

The ApplicationContext

The  ApplicationContext, enhances BeanFactory functionality in a more framework-oriented style.
The ApplicationContext interface, is located in the  org.springframework.context package. Its is derived from the BeanFactory interface, and provides all the functionality of BeanFactory. To allow working in a more framework-oriented fashion, using layering and hierarchical contexts, the context package also provides the following:
  • MessageSource:  providing access to messages in, i18n-style
  • Access to resources: such as URLs and files
  • Event propagation: to beans implementing the ApplicationListener interface
  • Loading of multiple (hierarchical) contexts: allowing each to be focused on one particular layer.
As the ApplicationContext includes all functionality of the BeanFactory, it is generally recommended that it be used over the BeanFactory, except for a few limited situations where memory consumption might be critical, and a few extra kilobytes might make a difference. 


ApplicationContext
ApplicationContext

No comments:

Post a Comment