Thursday, 2 February 2012

BeanFactory and ApplicationContext

BeanFactory:
 
As its name implies, a bean factory is an implementation of the Factory design pattern. That is, it is a class whose responsibility is to create and dispense beans. The BeanFactory is the actual container which instantiates, configures, and manages a number of beans. These beans typically collaborate with one another, and thus have dependencies between themselves. When a bean factory hands out objects, those objects are fully configured, are aware of their collaborating objects, and are ready to use.


BeanFactory is a workhorse that initializes beans and calls their life cycle methods. It should be noted that most life cycle methods only apply to singleton beans. Spring cannot manage prototype (non-singleton) life cycles. This is because, after they’re created, prototypes are handed off to the client and the container loses track of it. For prototypes, Spring is really just a replacement for the “new” operator.


A BeanFactory is represented by the interface org.springframework.beans.factory.BeanFactory, and it is having multiple implementations. The most commonly used simple BeanFactory implementation is org.springframework.beans.factory.xml.XmlBeanFactory. (This should be qualified with the reminder that ApplicationContexts are a subclass of BeanFactory, and most users end up using XML variants of ApplicationContext).


Although for most scenarios, almost all user code managed by the BeanFactory does not have to be aware of the BeanFactory, the BeanFactory does have to be instantiated somehow. This can happen via explicit user code such as:


XmlBeanFactory factory = new XmlBeanFactory(new FileSystemResource("spring.xml"));


or


XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource("beans.xml"));

ApplicationContext :

A Spring ApplicationContext is a subinterface of BeanFactory. It is somewhat similar to BeanFactory in terms that both define beans,bind them together and make them available on request.In its functioning and ApplicationContext has following advanced features:
  •  Resolving Messages, supporting internationalization(i18n).
  •  Support for an eventing mechanism, allowing application objects to publish events and they can register to be notified of events optionally.
  •  Supports generic loading and access of file resources like image files.
  •  Supports customization of container behavior through automatic recognition of special application-specific or generic, bean definitions.

No comments:

Post a Comment