BeanFactory vs ApplicationContext in Spring

Last Updated : 4 Apr, 2026

In the Spring Framework, the IoC (Inversion of Control) container is responsible for creating, configuring, and managing beans. There are two main container types:

  • BeanFactory (Basic container)
  • ApplicationContext (Advanced container)
DifferenceBetweenBeanFactory-andApplicationContext
BeanFactory

BeanFactory

BeanFactory, defined in org.springframework.beans.factory, is the basic IoC container in Spring. It provides core functionalities such as bean creation and dependency injection.

  • Beans are created only when requested
  • This behavior is known as lazy initialization
  • Suitable for lightweight or memory-constrained applications

Example:

Java
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;

BeanFactory factory = new XmlBeanFactory(
    new ClassPathResource("beans.xml"));

MyBean obj = (MyBean)factory.getBean("myBean");

Note: XmlBeanFactory is deprecated (since Spring 3.1) and removed in Spring 4.0 shown only for lazy initialization.In real apps, ApplicationContext is preferred over BeanFactory.

ApplicationContext

ApplicationContext, defined in org.springframework.context, is an advanced container that extends BeanFactory and provides additional enterprise-level features.

  • Eager initialization (by default)
  • Annotation-based configuration
  • Event handling
  • Internationalization (i18n)
  • Automatic post-processor registration

Example:

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

ApplicationContext context =
    new ClassPathXmlApplicationContext("beans.xml");

MyBean obj = context.getBean("myBean", MyBean.class);

BeanFactory vs ApplicationContext

Feature

BeanFactory

ApplicationContext

Definition

Fundamental container providing basic functionality for managing beans.

Advanced container extending BeanFactory with additional features.

Usage

Suitable for building standalone applications.

Suitable for building web applications, integrating with AOP modules, ORM, and distributed applications.

Bean Scopes Supported

Supports only Singleton and Prototype bean scopes.

Supports all types of bean scopes, including Singleton, Prototype, Request, Session, etc.

Annotation Support

Does not support annotations; requires configuration in XML files.

Supports annotation-based configuration for bean autowiring.

Internationalization

Does not provide internationalization (i18n) functionality.

Extends MessageSource interface to provide internationalization (i18n) functionality.

Event Handling

Does not support event publication.

Supports event handling via the ApplicationEvent class and ApplicationListener interface.

Bean Post Processing

Requires manual registration of BeanPostProcessors and BeanFactoryPostProcessors.

Automatically registers BeanFactoryPostProcessor and BeanPostProcessor at startup.

Initialization

Creates bean objects on demand using lazy initialization.

Loads all beans and creates objects at startup using eager initialization.

Resource Usage

Provides basic features requiring less memory, suitable for memory-critical standalone applications.

Provides basic and advanced features, suitable for enterprise applications, requiring more memory.

Comment

Explore