100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Java

The Spring Application Context

What the ApplicationContext is, how it manages the bean lifecycle, and how dependency injection resolves object graphs at startup.

Spring Boot FoundationsIntermediate10 min readJul 10, 2026
Analogies

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.

java
@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 new themselves.
  • @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

Was this page helpful?

Topics covered

#Java#SpringBootStudyNotes#WebDevelopment#TheSpringApplicationContext#Spring#Application#Context#Bean#StudyNotes#SkillVeris