Tuesday, August 7, 2012

BeanFactory vs ApplicationContext





ApplicationContext is much the same as a BeanFactory. 

Both 
  • load bean definitions
  • wire beans together
  • dispense beans upon request.

The ApplicationContext includes all functionality of the BeanFactory, it is generally recommended over the BeanFactory.
BeanFactory can still be used for light weight applications like mobile devices or applet based applications.

BeanFactory

ApplicationContext

·         An Interface that implements Factory-Pattern
·         A bean factory “lazily loads” all beans, deferring bean creation until the getBean() method is called.

·         Extends BeanFactory
·         An application context 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.
·         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.

XmlBeanFactory: A simple BeanFactory that loads a context definition file by way of a java.io.InputStream.
ClassPathXmlApplicationContext: it loads a context definition from an XML file located in the class path, treating context definition files as class path resources.
 e.g. ApplicationContext context = new ClassPathXmlApplicationContext(“foo.xml”);

FileSystemXmlApplicationContext: It loads a context definition from an XML file in the filesystem.
e.g ApplicationContext context = new FileSystemXmlApplicationContext(“c:/foo.xml”);

XmlWebApplicationContext: It loads context definitions from an XML file contained within a web application.


No comments:

Post a Comment