By WebNest Studio Engineering Team
Spring Framework Tutorial
ApplicationContext vs BeanFactory
Every Spring application is backed by a container that holds and manages beans. Spring actually offers two levels of container abstraction: the low-level BeanFactory interface, and the more capable ApplicationContext interface that extends it. Nearly every real Spring application — and certainly every Spring Boot application — uses an ApplicationContext, but understanding the distinction clarifies what the container is actually doing under the hood.
BeanFactory represents the most basic form of IoC container: it can create beans and resolve their dependencies, but that's essentially all it does, and it uses lazy initialization by default. ApplicationContext builds on top of BeanFactory and adds enterprise-level services — event publishing, internationalization support, AOP integration, environment/property abstraction, and eager initialization of singleton beans at startup — which is why it is the standard choice in practice.
BeanFactory — the Core Container
BeanFactory is the root interface for accessing a Spring IoC container. It provides basic dependency injection capability and lazily initializes beans — meaning a bean is not actually created until it is first requested via getBean(). This lazy behavior can be useful for very resource-constrained environments, but it also means configuration errors in a bean definition might not surface until much later in the application's life, when that specific bean is finally requested.
ApplicationContext — the Full-Featured Container
ApplicationContext extends BeanFactory and adds capabilities that almost every non-trivial application needs: automatic BeanPostProcessor and BeanFactoryPostProcessor registration, message resolution for internationalization (i18n), an event publication mechanism (ApplicationEventPublisher), and access to the Environment abstraction for reading properties and active profiles. Crucially, ApplicationContext eagerly instantiates all singleton beans at startup, which surfaces configuration problems immediately rather than at some unpredictable later point.
ClassPathXmlApplicationContext— loads XML bean definitions from the classpath (legacy XML-based configuration).AnnotationConfigApplicationContext— loads beans from Java @Configuration classes, the standard approach in modern Spring.WebApplicationContext— a specialization used within web applications, tied to the servlet lifecycle; Spring Boot configures this automatically.
Why ApplicationContext Is the Practical Default
In real projects you almost never instantiate a raw BeanFactory directly. Spring Boot's SpringApplication.run(), for instance, builds and returns a fully configured ApplicationContext behind the scenes. The eager singleton instantiation, built-in event system, and environment/profile support make ApplicationContext the correct choice for anything beyond an educational example of the bare container.
Example
Bootstrapping an ApplicationContext from Java configuration
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
class AppConfig {
@Bean
public GreetingService greetingService() {
return new GreetingService("Welcome to Spring");
}
}
class GreetingService {
private final String message;
GreetingService(String message) { this.message = message; }
void greet() { System.out.println(message); }
}
public class ContextDemo {
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
// Singleton beans, including greetingService, are already created by this point.
GreetingService service = context.getBean(GreetingService.class);
service.greet();
}
}
Welcome to Spring
Common Mistakes
- Assuming BeanFactory and ApplicationContext behave identically regarding initialization timing — BeanFactory is lazy by default, ApplicationContext eagerly creates singletons at startup.
- Manually instantiating BeanFactory in a modern application instead of relying on ApplicationContext (or Spring Boot's auto-configured context), losing event handling, i18n, and environment support.
- Forgetting to close a manually created ApplicationContext (via try-with-resources or context.close()) in a standalone application, leaving resources like connection pools unreleased.
- Confusing WebApplicationContext with a plain ApplicationContext when working with servlet-scoped beans (request/session scope), which only work inside a genuine web context.
Exercise
A teammate is about to make this mistake: Assuming BeanFactory and ApplicationContext behave identically regarding initialization timing — BeanFactory is lazy by default, ApplicationContext eagerly creates singletons at startup. Use BeanFactory — the Core Container to review the decision. Write a corrected implementation or design sketch that follows BeanFactory is the basic IoC container: lazy bean initialization, minimal features., then name one edge case you would check.
Show hints
- Start with BeanFactory — the Core Container and compare your approach with Bootstrapping an ApplicationContext from Java configuration. Identify the decision or behavior the lesson recommends.
- Walk through your scenario step by step. Confirm that it follows BeanFactory is the basic IoC container: lazy bean initialization, minimal features. and that it would catch the failure described by Assuming BeanFactory and ApplicationContext behave identically regarding initialization timing — BeanFactory is lazy by default, ApplicationContext eagerly creates singletons at startup..
Quick quiz
1. Which choice best describes the recommended approach in ApplicationContext vs BeanFactory?
- A. Assuming BeanFactory and ApplicationContext behave identically regarding initialization timing — BeanFactory is lazy by default, ApplicationContext eagerly creates singletons at startup.
- B. Manually instantiating BeanFactory in a modern application instead of relying on ApplicationContext (or Spring Boot's auto-configured context), losing event handling, i18n, and environment support.
- C. BeanFactory is the basic IoC container: lazy bean initialization, minimal features.
Show answer
C. This is the recommended approach: BeanFactory is the basic IoC container: lazy bean initialization, minimal features. Avoid: Assuming BeanFactory and ApplicationContext behave identically regarding initialization timing — BeanFactory is lazy by default, ApplicationContext eagerly creates singletons at startup.
Interview questions
Why does this principle matter in ApplicationContext vs BeanFactory: BeanFactory is the basic IoC container: lazy bean initialization, minimal features.
Show answer
BeanFactory is the basic IoC container: lazy bean initialization, minimal features. Apply this in BeanFactory — the Core Container; the example Bootstrapping an ApplicationContext from Java configuration shows one way to do it. Avoid Assuming BeanFactory and ApplicationContext behave identically regarding initialization timing — BeanFactory is lazy by default, ApplicationContext eagerly creates singletons at startup. and explain how your implementation or design prevents that failure.
Key Points to Remember
- BeanFactory is the basic IoC container: lazy bean initialization, minimal features.
- ApplicationContext extends BeanFactory and adds events, i18n, environment/profile support, and eager singleton initialization.
- AnnotationConfigApplicationContext is the standard way to bootstrap a context from Java @Configuration classes.
- Spring Boot always wires up an ApplicationContext (a WebApplicationContext for web apps) automatically.
Practice in the playground
Write and run your own code for this topic, or start from the exercise or an example. Runs plain Java 17. Frameworks such as Spring, and databases, are not available here. Your code for each topic is kept in this browser.
Run the examples
Change an input, predict the result, then compare it with the output. Explain why the result changes.
These examples need a JDK project with their libraries on your computer, so they can be copied but not run here. Codelab runs Python, FastAPI backends with SQLite, plain Java 17 and HTML/CSS/JavaScript.
Written by
WebNest Studio Engineering Team
Practical lessons from developers working with Java, Python, React, databases and production systems.
Led by Vansh Duggal, Founder, WebNest Studio · Full-stack & software engineering