Differences between BeanFactory and ApplicationContext

Read this article to know what are the differences between BeanFactory and ApplicationContext in Spring Framework. BeanFactory and ApplicationContext interfaces are acts as IOC container.
  • BeanFactory locates in org.springframework.beans.factory package and ApplicationContext locates in org.springframework.context path.
  • ApplicationContext build on top of BeanFactory. So, ApplicationContext will provide the additional functionalities over BeanFactory.
  • Simple integration with Spring AOP, Message Resource Handling (I18N), Event Propagation and application layer specific context(WebApplicationContext) are additional features available with ApplicationContext.
  • Because of these features ApplicationContext is better to use than the BeanFactory.
Instantiation of BeanFactory and ApplicationContext:
BeanFactory will use the XmlBeanFactory() class to instantiate. XmlBeanFactory() will use the Resource object to load the bean configuration file.

Resource resource = new ClassPathResource("spring-beans.xml");
BeanFactory factory = new XmlBeanFactory(resource);

ApplicationContext will use ClassPathXmlApplicationContext class to instantiate. ClassPathXmlApplicationContext uses the bean configuration file.

ApplicationContext context = new ClassPathXmlApplicationContext("spring-bean.xml");

Note: If any property declaration problem in bean configuration file, BeanFactory will fail at the time of creating that bean object only. But, ApplicationContext will load all configured beans at the time of loading the application. So, it will fail at the time of application startup only.

Popular posts from this blog

Multithreading in java with example

JNDI configuration for Tomcat 9 with Oracle

Prime, Fibonacci and Factorial number with example in java