The spring docs are great on this: 3.8.1. BeanFactory or ApplicationContext? They have a table with a comparison, I'll post a snippet.
The spring docs are great on this: 3.8.1. BeanFactory or ApplicationContext?. They have a table with a comparison, I'll post a snippet: Bean Factory Bean instantiation/wiring Application Context Bean instantiation/wiring Automatic BeanPostProcessor registration Automatic BeanFactoryPostProcessor registration Convenient MessageSource access (for i18n) ApplicationEvent publication So if you need any of the points presented on the Application Context side, you should use ApplicationContext.
Great, thanks, this answers the question perfectly! Not sure how I missed that in the documentation. – matt be Oct 28 '08 at 14:06 1 BeanFactory is lightweight, but if you're going to be using Spring "for real", you may as well go with the ApplicationContext: there is very little overhead involved if you don't use its fancy features, but they're still available for if/when you do use them.
– MetroidFan2002 Oct 29 '08 at 6:21.
To add onto what Miguel Ping answered, here is another section from the documentation that answers this as well: Short version: use an ApplicationContext unless you have a really good reason for not doing so. For those of you that are looking for slightly more depth as to the 'but why' of the above recommendation, keep reading. (posting this for any future Spring novices who might read this question).
To me, the primary difference to choose BeanFactory over ApplicationContext seems to be that ApplicationContext will pre-instantiate all of the beans. From the Spring docs: Spring sets properties and resolves dependencies as late as possible, when the bean is actually created. This means that a Spring container which has loaded correctly can later generate an exception when you request an object if there is a problem creating that object or one of its dependencies.
For example, the bean throws an exception as a result of a missing or invalid property. This potentially delayed visibility of some configuration issues is why ApplicationContext implementations by default pre-instantiate singleton beans. At the cost of some upfront time and memory to create these beans before they are actually needed, you discover configuration issues when the ApplicationContext is created, not later.
You can still override this default behavior so that singleton beans will lazy-initialize, rather than be pre-instantiated. 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 support classpath XML configuration.So BeanFactory and ApplicationContext 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: public class LazyLoadingXmlApplicationContext extends ClassPathXmlApplicationContext { public LazyLoadingXmlApplicationContext(String configLocations) { super(configLocations); } /** * Upon loading bean definitions, force beans to be lazy-initialized. * @see org.springframework.context.support.
AbstractXmlApplicationContext#loadBeanDefinitions(org.springframework.beans.factory.xml. XmlBeanDefinitionReader) */ @Override protected void loadBeanDefinitions(XmlBeanDefinitionReader reader) throws IOException { super. LoadBeanDefinitions(reader); for (String name : reader.getBeanFactory().
GetBeanDefinitionNames()) { AbstractBeanDefinition beanDefinition = (AbstractBeanDefinition) reader.getBeanFactory(). GetBeanDefinition(name); beanDefinition. SetLazyInit(true); } } }.
I would argue that if your unit tests are loading up your full Spring context, they aren't "unit tests", but integration tests. – matt be Mar 1 '10 at 12:50 Good point. In my case I actually needed to load beans from the context for performance and integration tests, and wrote "unit tests" out of habit.
I've edited my answer accordingly. – Lyle Mar 2 '10 at 15:11.
I think it's better to always use ApplicationContext, unless you're in a mobile environment like someone else said already. ApplicationContext has more functionality and you definitely want to use the PostProcessors such as RequiredAnnotationBeanPostProcessor, AutowiredAnnotationBeanPostProcessor and CommonAnnotationBeanPostProcessor, which will help you simplify your Spring configuration files, and you can use annotations such as @Required, @PostConstruct, @Resource, etc in your beans. Even if you don't use all the stuff ApplicationContext offers, it's better to use it anyway, and then later if you decide to use some resource stuff such as messages or post processors, or the other schema to add transactional advices and such, you will already have an ApplicationContext and won't need to change any code.
If you're writing a standalone app, load the ApplicationContext in your main method, using a ClassPathXmlApplicationContext, and get the main bean and invoke its run() (or whatever method) to start your app. If you're writing a web app, use the ContextLoaderListener in web. Xml so that it creates the ApplicationContext and you can later get it from the ServletContext, regardless of whether you're using JSP, JSF, JSTL, struts, Tapestry, etc.Also, remember you can use multiple Spring configuration files and you can either create the ApplicationContext by listing all the files in the constructor (or listing them in the context-param for the ContextLoaderListener), or you can just load a main config file which has import statements.
You can import a Spring configuration file into another Spring configuration file by using which is very useful when you programmatically create the ApplicationContext in the main method and load only one Spring config file.
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.
1)ApplicationContext is more preferred way than BeanFactory 2)In new spring versions BeanFactory is replaced with ApplicationContext. But still BeanFactory exists for backward compatability 3) ApplicationContext extends BeanFactory and has the following benifits a) it supports internationalization for text messages b) it supports event publication to the registered listeners c) access to the resources such as URL's and files.
The spring docs are great on this: 3.8.1. BeanFactory or ApplicationContext? .
They have a table with a comparison, I'll post a snippet.
To me, the primary difference to choose BeanFactory over ApplicationContext seems to be that ApplicationContext will pre-instantiate all of the beans. From the Spring docs.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.