The Spring Application Context
The ApplicationContext is the central container in Spring: it creates, configures, wires together, and manages the lifecycle of every bean in your application. When SpringApplication.run() executes, it bootstraps an ApplicationContext (typically an AnnotationConfigServletWebServerApplicationContext for a web app), scans for @Component, @Service, @Repository, and @Configuration classes, and instantiates a fully wired object graph before your application starts serving requests.
Cricket analogy: Like a franchise's team management office assembling the full playing XI, backroom staff, and support crew before the season starts, the ApplicationContext assembles every bean before the app serves its first request.
Bean Lifecycle
A bean's life follows a defined sequence: the container instantiates it, injects its dependencies (constructor or field injection), calls any @PostConstruct-annotated method, and the bean is then ready for use. On shutdown, the container calls any @PreDestroy-annotated method before discarding the bean. By default beans are singleton-scoped — one instance is shared for the entire ApplicationContext — though prototype, request, and session scopes are also available for different lifetimes.
Cricket analogy: Like a player's career arc from academy signing through debut, prime years, and eventual retirement ceremony, a bean moves through instantiation, injection, @PostConstruct readiness, use, and @PreDestroy cleanup.
Dependency Injection and Bean Resolution
When the ApplicationContext constructs a bean like OrderService that declares a constructor parameter of type OrderRepository, it looks up a bean of that type in its registry and passes it in — this is dependency injection. If multiple beans implement the same interface, Spring either throws a NoUniqueBeanDefinitionException or resolves the ambiguity using @Primary or @Qualifier to pick the right one, so you never call new on your own service classes.
Cricket analogy: Like a team selector automatically slotting a specialist spinner into the XI whenever the pitch conditions call for spin, without the captain manually recruiting one, the ApplicationContext automatically supplies the right dependency.
@Service
public class OrderService {
private final OrderRepository orderRepository;
// Constructor injection: Spring resolves OrderRepository from the context
public OrderService(OrderRepository orderRepository) {
this.orderRepository = orderRepository;
}
@PostConstruct
void init() {
System.out.println("OrderService ready");
}
@PreDestroy
void cleanup() {
System.out.println("OrderService shutting down");
}
public Order placeOrder(Order order) {
return orderRepository.save(order);
}
}Constructor injection is preferred over field injection (@Autowired on a field) because it makes dependencies explicit, immutable (via final fields), and testable without reflection. Field injection also hides circular dependency problems until runtime, whereas constructor injection surfaces them immediately as a startup failure.
- The ApplicationContext is Spring's container: it creates, wires, and manages the lifecycle of every bean.
- SpringApplication.run() bootstraps the context and performs component scanning before the app serves requests.
- Beans follow a lifecycle: instantiation, dependency injection, @PostConstruct, active use, then @PreDestroy on shutdown.
- Beans are singleton-scoped by default, with prototype, request, and session scopes available for other lifetimes.
- Dependency injection lets the container supply required collaborators instead of classes calling
newthemselves. - @Primary and @Qualifier resolve ambiguity when multiple beans implement the same interface.
- Constructor injection is preferred over field injection for explicitness, immutability, and easier testing.
Practice what you learned
1. What is the primary responsibility of the Spring ApplicationContext?
2. What is the default scope of a Spring bean?
3. What happens when a @PostConstruct-annotated method exists on a bean?
4. Why is constructor injection generally preferred over field injection?
5. What resolves ambiguity when multiple beans implement the same interface and one is required for injection?
Was this page helpful?
You May Also Like
What Is Spring Boot?
An introduction to Spring Boot as an opinionated, production-ready extension of the Spring Framework that removes boilerplate configuration.
Spring Boot Auto-Configuration
How Spring Boot inspects the classpath and existing beans to automatically configure sensible defaults, and how to customize or override them.
Spring Boot Project Structure
How a typical Spring Boot project is organized on disk, from Maven/Gradle build files to source packages and resources.
Related Reading
Related Study Notes in Web Development
Browse all study notesWebSockets Study Notes
Web Development · 30 topics
Web DevelopmentWebAssembly Study Notes
WebAssembly · 30 topics
Web DevelopmentgRPC Study Notes
Protocol Buffers · 30 topics
Web DevelopmentFlask Study Notes
Python · 30 topics
Web DevelopmentDjango Study Notes
Python · 30 topics
Web DevelopmentNext.js Study Notes
JavaScript · 30 topics