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.
To add onto what Miguel Ping answered, here is another section from the documentation that answers this as well:
(posting this for any future Spring novices who might read this question)
To me, the primary difference to choose
BeanFactory
overApplicationContext
seems to be thatApplicationContext
will pre-instantiate all of the beans. From the Spring docs:Given this, I initially chose
BeanFactory
for use in integration/performance tests since I didn't want to load the entire application for testing isolated beans. However -- and somebody correct me if I'm wrong --BeanFactory
doesn't supportclasspath
XML configuration. SoBeanFactory
andApplicationContext
each provide a crucial feature I wanted, but neither did both.Near as I can tell, the note in the documentation about overriding default instantiation behavior takes place in the configuration, and it's per-bean, so I can't just set the "lazy-init" attribute in the XML file or I'm stuck maintaining a version of it for test and one for deployment.
What I ended up doing was extending
ClassPathXmlApplicationContext
to lazily load beans for use in tests like so:I think it is worth mentioning that since Spring 3, if you want to create a factory, you can also use the
@configuration
annotation combined with the proper@scope
Your factory should be visible by Spring container using the
@ComponentScan
annotation or xml configurationSpring bean scopes article from baeldung site
Difference between BeanFactory and ApplicationContext are following:
Using BeanFactory:
BeanFactory beanfactory = new XMLBeanFactory(new FileSystemResource("spring.xml")); Triangle triangle =(Triangle)beanFactory.getBean("triangle");
Using ApplicationContext:
ApplicationContext context = new ClassPathXMLApplicationContext("spring.xml") Triangle triangle =(Triangle)beanFactory.getBean("triangle");
Refer this doc from Spring Docs:
http://static.springsource.org/spring/docs/3.2.x/spring-framework-reference/html/beans.html#context-introduction-ctx-vs-beanfactory
5.15.1 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.
ApplicationContext
is more preferred way thanBeanFactory
In new Spring versions
BeanFactory
is replaced withApplicationContext
. But stillBeanFactory
exists for backward compatabilityApplicationContext extends BeanFactory
and has the following benefits