The Wayback Machine - https://web.archive.org/web/20260217012759/https://www.geeksforgeeks.org/springboot/beanfactory-vs-applicationcontext-in-spring/

BeanFactory vs ApplicationContext in Spring

Last Updated : 14 Jan, 2026

Spring offers two main container abstractions:

  1. BeanFactory
  2. ApplicationContext

These are interfaces, not concrete classes and they represent two different levels of container capability. Below is the hierarchy of the BeanFactory(I) and ApplicationContext(I) with some of their implementation classes.

DifferenceBetweenBeanFactory-andApplicationContext
Interface Hierarchy
  • BeanFactory is the root interface
  • ApplicationContext extends BeanFactory
  • XmlBeanFactory implements BeanFactory (deprecated)
  • ClassPathXmlApplicationContext, FileSystemXmlApplicationContext, etc, implement ApplicationContext

This explains why Spring talks about BeanFactory vs ApplicationContext, not about specific classes.

1. 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.

Key Characteristics:

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

Declaration:

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

Note:

  • XmlBeanFactory is deprecated since Spring 3.1 and removed in Spring 4.0.
  • This example is shown only to demonstrate lazy initialization behavior.
  • BeanFactory itself is not deprecated, but it is rarely used directly in modern applications.
  • In real-world applications, ApplicationContext is preferred.

Example Using BeanFactory (Lazy Initialization)

Step 1: Bean Class (WelcomeBean.java)

Java
public class WelcomeBean {

    private String name;

    public void setName(String name) {
        this.name = name;
    }

    public String welcomeMsg() {
        return "Welcome " + name;
    }

    public void init() {
        System.out.println("Welcome Bean is initialized!!");
    }

    public void destroy() {
        System.out.println("Welcome Bean is destroyed!!");
    }
}

Step 2: XML Configuration (beans.xml)

Java
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="welcomeBean"
          class="WelcomeBean"
          init-method="init"
          destroy-method="destroy">
        <property name="name" value="Geek"/>
    </bean>

</beans>

Step 3: Test Class (WelcomeBeanTest.java)

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

public class WelcomeBeanTest {

    public static void main(String[] args) {

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

        System.out.println("Before getBean() method");

        WelcomeBean bean = (WelcomeBean) factory.getBean("welcomeBean");
        System.out.println(bean.welcomeMsg());

        factory.destroySingletons();
    }
}

Output:

BFO

Explanation:

  • The bean is not initialized at startup
  • Initialization happens only when getBean() is called
  • This behavior is called lazy initialization
  • XmlBeanFactory is deprecated and used only for demonstration

Functionalities

1. Bean Autowiring in BeanFactory

  • Uses XML-based configuration
  • Supports the autowire attribute
  • Annotation-based autowiring is not enabled automatically
XML
<bean id="welcomeBean"
      class="com.geeks.beans.WelcomeBean"
      autowire="byName"/>

2. BeanPostProcessor Handling in BeanFactory

  • BeanPostProcessor and BeanFactoryPostProcessor must be manually registered
  • BeanFactory does not automatically detect post-processors

3. Bean Initialization Example (Lazy Initialization)

Behavior:

  • Bean is initialized only when getBean() is called
  • Initialization is on-demand

Execution Result:

  • Initialization messages appear after getBean()
  • This confirms lazy initialization

2. ApplicationContext:

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

Key Features

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

Declaration:

Java
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

Example Using ApplicationContext (Eager Initialization)

Step 1: Bean Class (HelloBean.java)

Java
public class HelloBean {

    private String name;

    public void setName(String name) {
        this.name = name;
    }

    public String helloMsg() {
        return "Hello " + name;
    }

    public void init() {
        System.out.println("Hello Bean is initialized!!");
    }

    public void destroy() {
        System.out.println("Hello Bean is destroyed!!");
    }
}

Step 2: XML Configuration (applicationContext.xml)

XML
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="helloBean"
          class="HelloBean"
          init-method="init"
          destroy-method="destroy">
        <property name="name" value="Geek"/>
    </bean>

</beans>

Step 3: Test Class (HelloBeanTest.java)

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

public class HelloBeanTest {

    public static void main(String[] args) {

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

        System.out.println("Before getBean() method");

        HelloBean bean = (HelloBean) context.getBean("helloBean");
        System.out.println(bean.helloMsg());

        ((ClassPathXmlApplicationContext) context).close();
    }
}

Output:

ACo
Output image

Explanation:

  • The bean is initialized as soon as the ApplicationContext is created
  • Initialization happens before getBean() is called
  • This behavior is called eager initialization
  • This is the recommended approach for modern Spring applications

Functionalities

Bean Autowiring in ApplicationContext

  • Fully supports annotation-based autowiring
  • Supports @Autowired on fields, setters, and constructors

BeanPostProcessor Handling in ApplicationContext

  • Automatically detects and registers:
    • BeanPostProcessor
    • BeanFactoryPostProcessor
  • Safer and more convenient than BeanFactory

Bean Initialization Example (Eager Initialization)

Behavior:

  • All singleton beans are initialized at application startup
  • Initialization happens before getBean() is called

Execution Result:

  • Initialization messages appear before getBean()
  • This confirms eager initialization

Internationalization Support

ApplicationContext extends MessageSource, enabling internationalization support.

Java
String getMessage(String code,
                  Object[] args,
                  String defaultMessage,
                  Locale locale);

Difference Between BeanFactory and ApplicationContext

Below is the difference table of Spring BeanFactory and 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.

Suggested Quiz

0 Questions

Quiz Completed Successfully

Your Score : 0/0

Accuracy : 0%

Comment

Explore