BeanFactory vs ApplicationContext in Spring


In this short article, we will quickly discuss the difference between the BeanFactory and ApplicationContext container levels and the implications of bootstrapping.
Let me first define what is the Spring Container?
The Spring container is responsible for instantiating, configuring, and assembling the Spring beans. The container gets its instructions on what objects to instantiate, configure, and assemble by reading configuration metadata. The configuration metadata is represented in XML, Java annotations, or Java code. It lets you express the objects that compose your application and the rich interdependencies between those objects.
Both BeanFactory and ApplicationContext provides a way to get a bean from Spring IOC container by calling getBean("bean name"), but there is some difference in there working and features provided by them.
The below diagram shows the implementations of  BeanFactory and ApplicationContext interfaces:

The BeanFactory interface

The BeanFactory is a root interface for accessing the Spring container. To access the Spring container, we will use Spring’s Dependency Injection functionality using this BeanFactory interface and its subinterfaces.
Features: Bean instantiation/wiring
Usually, the implementations use lazy loading, which means that Beans are only instantiating when we directly calling them through the getBean() method.
The most used implementation of the BeanFactory interface is the XmlBeanFactory class.  
Example to get a bean through BeanFactory:
package net.javaguides.spring.ioc;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Application {
    public static void main(String[] args) {
        XmlBeanFactory factory = new XmlBeanFactory (new ClassPathResource("applicationContext.xml"));
    }
}
Check out a complete example using BeanFactory at https://www.javaguides.net/2018/10/spring-beanfactory-interface-example.html

The ApplicationContext interface

The ApplicationContext is the central interface within a Spring application for providing configuration information to the application.
It implements the BeanFactory interface. Hence ApplicationContext includes all functionality of the BeanFactory and much more!
Its main function is to support the creation of big business applications.
Features:
  • Bean instantiation/wiring
  • Automatic BeanPostProcessor registration
  • Automatic BeanFactoryPostProcessor registration
  • Convenient MessageSource access (for i18n)
  • ApplicationEvent publication
Uses eager loading, so every bean instantiate after the ApplicationContext started up.
ApplicationContext Example:
package net.javaguides.spring.ioc;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Application {
    public static void main(String[] args) {
        ApplicationContext  context = new AnnotationConfigApplicationContext(AppConfig.class);
        context.close();
    }
}

Summary

  • One main between BeanFactory and ApplicationContext is that BeanFactory only instantiates bean when we call getBean() method while ApplicationContext instantiates singleton bean when the container is started, It doesn't wait for getBean() method to be called.
  • Below diagram summarize the features provided by BeanFactory and ApplicationContext interfaces and implementations:


Java EE Tutorials

Free Spring Boot Tutorial | Full In-depth Course | Learn Spring Boot in 10 Hours


Watch this course on YouTube at Spring Boot Tutorial | Fee 10 Hours Full Course