An IOC container is responsible for maintaining the lifecycle(managing, configuring etc) of the beans. The BeanFactory and ApplicationContext are two types of IOC containers provided by Spring. Let’s take a look:
BeanFactory(I)
It is basic IOC container hence lightweight.
It can only be configured using xml.
It does not support internationalization(i18n).
It does not support annotations.
There is only one class implementing BeanFactory i.e. XMLBeanFactory
It does lazy initialization of the beans. It only creates the beans when getBean() method is called.
How is it used
BeanFactory beanfactory = new XMLBeanFactory(new FileSystemResource("project-config.xml"));
ApplicationContext(I)
It is an advanced inversion of control container. It is built on top of BeanFactory hence has all the features of BeanFactory as well.
It can be configured by XML as well as Annotations.
It supports internationalization(i18n).
It supports annotations.
Classes implementing ApplicationContext are ClassPathXMLApplicationContext, FileSystemXMLApplicationContext, XMLWebApplicationContext and AnnotationConfigWebApplicationContext.
It does eager initialization of the bean. It initializes the beans at the time of server startup.
It does automatic registration of Bean postprocessors.
It does automatic registration of BeanFactoryPostProcessor.
How is it used
1. ClassPathXmlApplicationContext
ApplicationContext context = new ClassPathXmlApplicationContext("project-config.xml");
2. FileSystemXMLApplicationContext
ApplicationContext context = new FileSystemXMLApplicationContext("C:/Users/prgrmmng/ HelloSpring/src/Beans.xml");
3.XMLWebApplicationContext
ApplicationContext context = new new XmlWebApplicationContext().setConfigLocation(location);
4.AnnotationConfigWebApplicationContext
ApplicationContext context = new AnnotationConfigWebApplicationContext();
context.register(AppContextConfig.class);