Spring 3.0 – BeanFactory vs ApplicationContext
ApplicationContext is much the same as a BeanFactory. Both load bean definitions, wire beans together, and dispense beans upon request.
But there are differences, ApplicationContext extends BeanFactory by providing additional features, see below the differences.
- An Interface that implements Factory-Pattern
- A bean factory “lazily loads” all beans, deferring bean creation until the getBean() method is called.
- Extends BeanFactory
- An application context preloads all singleton beans upon context startup. By preloading singleton beans, you ensure that they will be ready to use when needed—your application won’t have to wait for them to be created.
- Application contexts provide a means for resolving text messages, including support for internationalization (I18N) of those messages.
- Application contexts provide a generic way to load file resources, such as images.
- Application contexts can publish events to beans that are registered as listeners.
- XmlBeanFactory: A simple BeanFactory that loads a context definition file by way of a java.io.InputStream.
- ClassPathXmlApplicationContext: it loads a context definition from an XML file located in the class path, treating context definition files as class path resources. e.g. ApplicationContext context = new ClassPathXmlApplicationContext(“foo.xml”);
- FileSystemXmlApplicationContext: It loads a context definition from an XML file in the filesystem. e.g ApplicationContext context = new FileSystemXmlApplicationContext(“c:/foo.xml”);
- XmlWebApplicationContext: It loads context definitions from an XML file contained within a web application.