BeanFactory vs ApplicationContext in Spring: Key Differences Explained

This article compares Spring's BeanFactory and ApplicationContext, detailing their roles, loading strategies, feature sets, code examples, and recommended usage scenarios, helping developers choose the appropriate container for their projects.

java1234
java1234
java1234
BeanFactory vs ApplicationContext in Spring: Key Differences Explained

BeanFactory Overview

BeanFactory is the most basic Spring IoC container interface. It provides core functions such as managing bean lifecycles, performing dependency injection, and offering a getBean(String name) method for retrieving bean instances.

Key characteristics : provides only the essential IoC features, uses lazy loading by default, and consumes fewer resources, making it suitable for lightweight environments.

ApplicationContext Overview

ApplicationContext extends BeanFactory and adds many enterprise‑level capabilities. In addition to the core IoC features, it supports internationalization (i18n), an event mechanism, automatic BeanPostProcessor registration, AOP integration, and profile‑based configuration.

Common implementations include ClassPathXmlApplicationContext, FileSystemXmlApplicationContext, and AnnotationConfigApplicationContext. In modern Spring projects, ApplicationContext is the most frequently used container.

Major Differences

Hierarchy : BeanFactory is the base container; ApplicationContext is a sub‑interface of BeanFactory.

Loading strategy : BeanFactory loads beans lazily, while ApplicationContext loads all singleton beans eagerly at startup.

Internationalization : not supported by BeanFactory, supported by ApplicationContext.

Event mechanism : absent in BeanFactory, present in ApplicationContext.

BeanPostProcessor registration : manual in BeanFactory, automatic in ApplicationContext.

Typical usage : BeanFactory for lightweight, resource‑constrained apps; ApplicationContext for enterprise‑level applications.

BeanFactory Example Code

1. Define a bean class

public class UserService {
    public void sayHello() {
        System.out.println("Hello Spring BeanFactory");
    }
}

2. beans.xml configuration

<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="userService" class="com.demo.UserService"/>
</beans>

3. Load beans with BeanFactory

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

public class BeanFactoryTest {
    public static void main(String[] args) {
        BeanFactory factory = new XmlBeanFactory(new ClassPathResource("beans.xml"));
        UserService userService = (UserService) factory.getBean("userService");
        userService.sayHello();
    }
}

In this example the bean is created only when getBean is called, demonstrating lazy loading.

ApplicationContext Example Code

Code

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

public class ApplicationContextTest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        UserService userService = context.getBean("userService", UserService.class);
        userService.sayHello();
    }
}

When the ApplicationContext starts, all singleton beans are instantiated immediately (eager loading).

Practical Recommendations

When to use BeanFactory

Memory resources are extremely limited.

Only basic IoC functionality is required.

Features such as events and internationalization are not needed.

These situations are rare in typical enterprise development.

When to use ApplicationContext

Most Spring projects benefit from its richer feature set.

It simplifies development and supports enterprise‑level concerns.

Commonly used in Spring Boot, Spring MVC, and microservice architectures.

Therefore, for the majority of applications, using ApplicationContext as the Spring container is recommended.

JavaIoCSpringdependency-injectionApplicationContextbeanfactory
java1234
Written by

java1234

Former senior programmer at a Fortune Global 500 company, dedicated to sharing Java expertise. Visit Feng's site: Java Knowledge Sharing, www.java1234.com

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.