BeanFactory or ApplicationContext?


Use an ApplicationContext unless you have a good reason for not doing so.

Because the ApplicationContext includes all functionality of the BeanFactory, it is generally recommended over the BeanFactory, except for a few situations such as in an Applet where memory consumption might be critical and a few extra kilobytes might make a difference. However, for most typical enterprise applications and systems, the ApplicationContext is what you will want to use. Spring 2.0 and later makes heavy use of the BeanPostProcessor extension point (to effect proxying and so on). If you use only a plain BeanFactory, a fair amount of support such as transactions and AOP will not take effect, at least not without some extra steps on your part. This situation could be confusing because nothing is actually wrong with the configuration.

The following table lists features provided by the BeanFactory and ApplicationContext interfaces and implementations.

Points to be noted:

  • Application Context Container is an enhancement of Bean Factory Container with some advantages. Its features are as follows.
  • Pre-initialization of beans by default. [Early loading]. It means in out spring configuration file we may have any number if bean tags. Objects will be created sequentially for each and every bean class as specified in the sequence of tags in xml file.
  • Ability to read values of bean properties for properties file.
  • Supports internationalization (i18n).
  • Gives the ability to work with events and listeners.
  • Org.springframework.context.ApplicationContext interface is the sub interface org.springframework.bens.factory.BeanFactory interface.
  • Activating application context interface is nothing but creating object of class that implements org.springframework.context.ApplicationContext interface.
  • There are three regularly used implementation classes of application context interface. By creating object for any of these three classes we can activate Application Context container.
  • This container is available in JEE module.
  • Org.springframework It activates application context container by locating given spring configuration file in the specified path.

Ex. FileSystemXmlApplicationContext ctx = new FileSystemXmlApplicationContext (c:\Spring.config.xml);

  •  Org.springframework It activates application context container by locating spring configuration file in the same working directory or from jar files added in class path.

Ex. ClassPathXmlApplicationContext ctx. = new ClassPathXmlApplicationContext (“spring.conf.xml”);

  • Org.springframework this class activates application context container by locating spring configuration file in deploymentdirectory structure of web application by default in WEB-INF folder.

Ex. XmlWebApplicationContext ctx = new XmlWebApplicationContext (“spring.cfg.xml”);

  • In real time applications we can find regularly working with application context container rather than working with bean factory container.
  • Application context container can perform all modes of dependency injection like ban factory container.
  • Application context container performs pre initialization on all singleton scoped on spring bean class of spring configuration file at the moment of application context container gets activated.
  • Pre-initialization means creating spring bean class objects immediately after the application context container activation.
  • BeanFactory cannot perform this pre instantiation on spring bean classes.
  • When we call factory.getBean() the bean factory container immediately creates the object and used the same for further requests on same id- value.
  • When we call ctx.getBean() then the application context container gives access to bean id related spring bean class object which is created at pre-insanitation process.
  • Application context container can perform pre-instantiation only on singleton scoped spring bean classes that are configured in spring configuration file.
  • If we give scope a prototype then objet will not be created of bean at the time of pre-instantiation.
  • If the object is not created at pre-initialization then it will be created after receiving the call along with it dependent objects.
  • If single ton scope class property has prototype scoped class object as dependent value then the application context container also creates prototype scoped spring bean class object during pre-initialization process along with single ton scope bean class.
  • It happens to satisfy dependency injection needs done on singleton scope bean class properties.