DEV Community

Hunor Vadasz-Perhat
Hunor Vadasz-Perhat

Posted on

spring-008: how-spring-boot-determines-application-context

1. How Does Spring Boot Determine the Application Context Implementation?

When calling:

SpringApplication.run(MySpringBootApp.class, args);
Enter fullscreen mode Exit fullscreen mode

Spring Boot automatically determines the correct ApplicationContext implementation based on:

  1. The classpath (dependencies present in the project).
  2. The type of application being run (web or non-web).

Determination Process

Internally, SpringApplication uses the following logic to select the right ApplicationContext:

  • If Spring MVC or Spring WebFlux is present (spring-boot-starter-web or spring-boot-starter-webflux is included in dependencies):

    • It initializes a web-based application context:
    • Servlet-based (spring-boot-starter-web present):AnnotationConfigServletWebServerApplicationContext (for Spring MVC apps with embedded Tomcat, Jetty, or Undertow).
    • Reactive (spring-boot-starter-webflux present):AnnotationConfigReactiveWebServerApplicationContext (for WebFlux applications).
  • If neither spring-boot-starter-web nor spring-boot-starter-webflux is present:

    • It initializes a non-web application context: → AnnotationConfigApplicationContext.

Example: Web Application Context

@SpringBootApplication
public class WebApplication {
    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(WebApplication.class, args);
        System.out.println("Context Type: " + context.getClass().getName());
    }
}
Enter fullscreen mode Exit fullscreen mode

If spring-boot-starter-web is included, the output will be:

Context Type: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext
Enter fullscreen mode Exit fullscreen mode

Example: Non-Web Application Context

If you remove spring-boot-starter-web, the output changes to:

Context Type: org.springframework.context.annotation.AnnotationConfigApplicationContext
Enter fullscreen mode Exit fullscreen mode

2. Why Is It Important to Initialize the Application Context?

The application context is the core container that manages the lifecycle of beans and configurations in a Spring Boot application. Initializing it is critical for the following reasons:

1. Bean Management

  • The application context registers and manages beans, allowing dependency injection (@Autowired).
  • Without an application context, Spring wouldn't know how to instantiate and inject dependencies.

2. Auto-Configuration

  • The @EnableAutoConfiguration mechanism relies on the application context.
  • It scans the classpath and automatically configures Spring components based on dependencies.

3. Lifecycle and Event Management

  • The application context publishes lifecycle events (ApplicationReadyEvent, ApplicationStartedEvent).
  • It listens for shutdown signals and properly manages resources.

4. Embedded Web Server Support

  • For web applications, the application context starts an embedded server (Tomcat, Jetty, Undertow).
  • Without it, Spring Boot cannot serve HTTP requests.

5. Environment and Property Management

  • The context loads configuration properties from application.properties or application.yml.
  • It manages profiles (@Profile) and environment-specific settings.

3. Practical Effects of Choosing the Right Application Context

Choosing the correct application context impacts how your application behaves in the following ways:

1. Determines Whether an Embedded Web Server Starts

  • If the wrong context is chosen, your application might not start as a web application.
  • A web app requires AnnotationConfigServletWebServerApplicationContext, which bootstraps Tomcat/Jetty.

2. Controls Component Scanning and Dependency Injection

  • The context initializes and injects dependencies only within its scope.
  • For example, a non-web context will not scan or initialize controllers (@RestController won't work).

3. Enables or Disables Auto-Configuration

  • Spring Boot automatically applies configurations based on the selected context.
  • Example: If the web context is selected, Spring Boot auto-configures MVC components.

4. Affects How Beans Are Managed and Loaded

  • A web context pre-configures a DispatcherServlet, which handles HTTP requests.
  • A non-web context does not, meaning you cannot handle web requests without extra configuration.

Summary

Aspect Effect of Application Context
Bean Management Initializes and manages dependencies (@Autowired)
Web Server Starts embedded Tomcat/Jetty (if web context)
Auto-Configuration Applies automatic configurations based on classpath
Lifecycle Management Handles startup/shutdown events
Profile and Environment Loads properties, manages profiles (@Profile)
Dependency Injection Scope Determines which beans and controllers are available

Final Thoughts

  • Spring Boot automatically chooses the correct application context based on the classpath.
  • Web applications require a web-based context (AnnotationConfigServletWebServerApplicationContext).
  • Non-web applications use a standard annotation-based context (AnnotationConfigApplicationContext).
  • Initializing the right context ensures proper dependency injection, auto-configuration, and lifecycle management.🚀

Top comments (0)