Sunday, May 29, 2016

Advantage of ApplicationContext over BeanFactory in Spring


         Beans and Context are two most important packages in Spring Framework. Both BeanFactory and ApplicationContext are used for the same purpose to work as Spring IOC Container.
         The BeanFactory provides advanced configuration mechanism capable of managing beans whereas the ApplicationContext builds on top of BeanFactory with some addition functionality such as event propagation, message resource handling & easier integration with Springs AOP features.
         Beans can be configured in the Spring IoC container through XML files, Properties files, Annotations, or even APIs. Spring Container reads these beans by using BeanFactory or ApplicationContext. 
         If we have defined our Beans in the Spring.xml, then BeanFactory or ApplicationContext needs to read the Spring.xml to load the Beans in container. 

Bean factory: 

        It is a container which instantiates, configures, and manages a number of beans which establishes dependencies between themselves. 
        BeanFactory interface's implementation class XmlBeanFactory is to create and initialize the factory bean and ClassPathResource to load the bean configuration file.To load the Bean configuration in the container  using BeanFactory, It requires ResourceObject interface which will provide the XML file.

Example: 


ApplicationContext:

       An interface for providing configuration information to an application. Likely the most advanced spring container, provides all the features of BeanFactory along with extra features to resolve textual messages from a Properties File and publish application events to interested Event listeners.
      ApplicationContext implementations are FileSystemXmlApplicationContext, ClassPathXmlApplicationContext, WebXmlApplicationContext .


       Here Spring.xml is the spring configuration file and “sringdemo” is a bean defined in that spring configuration file.we have used ClassPathXmlApplicationContext, which is an implementation of ApplicationContext interface in Spring.

Here are some basic differences:

  1. BeanFactory is core components of Spring IoC container and is recommended for lightweight framework like Mobile and Applet
  2. ApplicationContext extends BeanFactory so ApplicationContext is more powerful than BeanFactory. ApplicationContext is preffered to be used while developing J2EE Entreprise applications
  3. ApplicationContext allows more than one config files to exist while BeanFactory only permits one.
  4. ApplicationContext can print Events to Beans registered as listeners. This feature is not supported by BeanFactory.
  5. ApplicationContext also provides support for application of Life-cycle events, internationalization messages and validation and also provides services like EJB integration, remoting, JNDI access and scheduling.These features too are not supported by Bean Factory.

0 on: "Advantage of ApplicationContext over BeanFactory in Spring"