I'm pretty new to the Spring Framework, I've been playing around with it and putting a few samples apps together for the purposes of evaluating Spring MVC for use in an upcoming company project. So far I really like what I see in Spring MVC, seems very easy to use and encourages you to write classes that are very unit test-friendly.
Just as an exercise, I'm writing a main method for one of my sample/test projects. One thing I'm unclear about is the exact differences between BeanFactory
and ApplicationContext
- which is appropriate to use in which conditions?
I understand that ApplicationContext
extends BeanFactory
, but if I'm just writing a simple main method, do I need the extra functionality that ApplicationContext
provides? And just exactly what kind of extra functionality does ApplicationContext
provide?
In addition to answering "which should I use in a main() method", are there any standards or guidelines as far as which implementation I should use in such a scenario? Should my main() method be written to depend on the bean/application configuration to be in XML format - is that a safe assumption, or am I locking the user into something specific?
And does this answer change in a web environment - if any of my classes needed to be aware of Spring, are they more likely to need ApplicationContext
?
Thanks for any help. I know a lot of these questions are probably answered in the reference manual, but I'm having a hard time finding a clear breakdown of these two interfaces and the pros/cons of each without reading thru the manual with a fine-tooth comb.
do use BeanFactory for non-web applications because it supports only Singleton and Prototype bean-scopes.
While ApplicationContext container does support all the bean-scopes so you should use it for web applications.
For the most part, ApplicationContext is preferred unless you need to save resources, like on a mobile application.
I'm not sure about depending on XML format, but I'm pretty sure the most common implementations of ApplicationContext are the XML ones such as ClassPathXmlApplicationContext, XmlWebApplicationContext, and FileSystemXmlApplicationContext. Those are the only three I've ever used.
If your developing a web app, it's safe to say you'll need to use XmlWebApplicationContext.
If you want your beans to be aware of Spring, you can have them implement BeanFactoryAware and/or ApplicationContextAware for that, so you can use either BeanFactory or ApplicationContext and choose which interface to implement.
Feature Matrix of Bean Factory vs Application Context sourced from spring docs
Screenshot of features of BeanFacotry and ApplicationContext
FileSystemXmlApplicationContext
Beans loaded through the full path.ClassPathXmlApplicationContext
Beans loaded through the CLASSPATHXMLWebApplicationContext
andAnnotationConfigWebApplicationContext
beans loaded through the web application context.AnnotationConfigApplicationContext
Loading Spring beans from Annotation based configuration.example:
ApplicationContext
is the container initialized by aContextLoaderListener
orContextLoaderServlet
defined in aweb.xml
andContextLoaderPlugin
defined instruts-config.xml
.In summary:
The ApplicationContext includes all functionality of the BeanFactory. It is generally recommended to use the former.
There are some limited situations such as in a Mobile application, where memory consumption might be critical.
In that scenarios, It can be justifiable to use the more lightweight BeanFactory. However, in the most enterprise applications, the ApplicationContext is what you will want to use.
For more, see my blog post:
Difference between BeanFactory and ApplicationContext in Spring – The java spring blog from the basics
Basically we can create spring container object in two ways
both are the interfaces
using implementation classes we can create object for spring container
coming to the differences
BeanFactory
Does not support the Annotation based dependency Injection.
Doesn't Support I18N
By default its support Lazy loading
it doesn't allow configure to multiple configuration files.
ex: BeanFactory context=new XmlBeanFactory(new Resource("applicationContext.xml"));
ApplicationContext
Support Annotation based dependency Injection.-@Autowired, @PreDestroy
Support I18N
its By default support Aggresive loading.
it allow to configure multiple configuration files.
ex:
ApplicationContext context=new ClasspathXmlApplicationContext("applicationContext.xml");