When should we use Bean factory and when should we use Application Context while developing Spring Applications?
-
I am learning Spring and I am reading "Spring in Action" book. It says, "Thereâs no single Spring container. Spring comes with several container implementations that can be categorized into two distinct types. a) Bean factories (defined by the org.springframework.beans.factory.BeanFactory interface) are the simplest of containers, providing basic support for DI. b) Application contexts (defined by the org.springframework.context.ApplicationContext interface) build on the notion of a bean factory by providing application framework services, such as the ability to resolve textual messages from a properties file and the ability to publish application events to interested event listeners." On the other side, Application context also helps in loading Spring beans and wire them together. So, could you give me some examples of when to use Bean factory and when to use Application Context?
-
Answer:
I would recommend you stick to using ApplicationContext in most cases. It provides a few "extras" on top of the BeanFactory that are generally worthwhile: Built-in message internationalization, via the MessageSource interface. A built-in event system, allowing you to get events when a context is started, stopped, or refreshed, for instance. You can also define your own events to go through this system. Convenient access to low-level resources, through the ResourceLoader interface. In addition, the ApplicationContext supports automatic registration of BeanPostProcessor and BeanFactoryPostProcessor components, whereas the BeanFactory does not; extra "glue code" is required to register those components with a BeanFactory. Some of Spring's other features, like transaction support and aspect-oriented programming (AOP), depend on these, and won't work right if you're using a BeanFactory instead of an ApplicationContext. If you're writing a Web application, it's dead simple to create an ApplicationContext, too; just declare a ContextLoaderListener as a listener in your Web app, and declare the context parameter "contextConfigLocation" to point to your XML file(s) defining the context. The only situation where a BeanFactory might be more appropriate than an ApplicationContext is if you're in a severely memory-constrained environment (such as a Java applet or mobile application) and you absolutely need to get rid of any kilobytes of overhead you possibly can, and this is worth the tradeoff in terms of functionality. Otherwise, minimize your own headaches and stick with the ApplicationContext.
Eric Bowersox at Quora Visit the source
Related Q & A:
- When do you use 'para' and when do you use 'por?Best solution by Yahoo! Answers
- When should I use serialization library in boost?Best solution by ibm.com
- It is an error to use a section registered as allowDefinition='MachineToApplication' beyond application?Best solution by Stack Overflow
- When do I use TSA locks for my luggage?Best solution by blog.tsa.gov
- When can I use the chat feature within Yahoo games?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.