Tuesday, 14 April 2009

BeanFactory and ApplicationContext in Spring

Basis for Spring's Dependency Injection feature is provided by two most fundamental and important packages in Spring namely org.springframework.beans and org.springframework.context packages.

The BeanFactory from org.springframework.beans provides an advanced configuration mechanism capable of managing beans (objects) of any nature, using potentially any kind of storage facility.

The ApplicationContext from org.springframework.context package builds on top of the BeanFactory (it's a subclass) and adds other functionality such as easier integration with Springs AOP features, message resource handling (for use in internationalization), event propagation, declarative mechanisms to create the ApplicationContext and optional parent contexts, and application-layer specific contexts such as the WebApplicationContext, among other enhancements.

BeanFactory is an internal interface that 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 complete superset of a BeanFactory, and any description of BeanFactory capabilities and behavior should be considered to apply to ApplicationContexts as well.

How to determine which one is best suited in a particular situation. BeanFactory or ApplicationContext???

Normally when building most applications in a J2EE-environment, the best option is to use the application context for every application unless the resources of this application are restricted, such as when running in an applet or a mobile device, and you don't need all the features of the ApplicationContext.


Hope it will be useful...

4 comments:

Prashant Jalasutram said...

Nice Article Sneha.

Thanks
Prashant
http://www.prashantaboutindia.blogspot.com/

Harshani said...

Sneha! Its a very nice article for beginners.Thanks

satish said...

Hi sneha,I am new to spring .I have setup simple login application.But I did not use it any where.would u tell me where we need to use with example code( with simple programme) ......

Sandeep said...

I don't know who copied. The same post is here also:

http://www.techfaq360.com/answer.jsp?catId=8&type=answer&index=23&qiid=359