Java and Spring Level 2 This level · 57 of 84
Medium java spring

What happens internally when you add spring-boot-starter-web?

The 30 second answer

The starter itself just brings in dependencies: Spring MVC, Jackson, message converters and embedded Tomcat. In a standard setup, those classes let Spring Boot recognize a servlet web application and activate the matching auto-configurations. They supply a TomcatServletWebServerFactory, a DispatcherServlet and the MVC infrastructure, unless you've provided replacements. The application context starts the server during refresh, using port 8080 unless you configure another port.

Could you say this in an interview?

Step by step:

1. The starter only adds jars. In Spring Boot 4 the web starter is a thin Gradle module:

dependencies {
    api(project(":starter:spring-boot-starter-jackson"))
    api(project(":starter:spring-boot-starter-tomcat"))
    api(project(":module:spring-boot-http-converter"))
    api(project(":module:spring-boot-webmvc"))
}

spring-boot-starter-web/build.gradle. In Spring Boot 4 this starter is deprecated in favour of spring-boot-starter-webmvc, which has the same content plus the base starter (build.gradle).

2. The application type is deduced. WebApplicationType checks the classpath. If jakarta.servlet.Servlet and ConfigurableWebApplicationContext are present, the app is a SERVLET application (WebApplicationType.java L43-L67). Spring Boot then creates a servlet web server application context.

3. Conditions now match. The Tomcat auto-configuration is guarded like this:

@AutoConfiguration
@ConditionalOnClass({ ServletRequest.class, Tomcat.class, UpgradeProtocol.class, TomcatServletWebServerFactory.class })
@ConditionalOnWebApplication(type = Type.SERVLET)
public final class TomcatServletWebServerAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean(value = ServletWebServerFactory.class, search = SearchStrategy.CURRENT)
    TomcatServletWebServerFactory tomcatServletWebServerFactory(...) { ... }
}

TomcatServletWebServerAutoConfiguration.java L41-L65

DispatcherServletAutoConfiguration is similar: @ConditionalOnWebApplication(type = SERVLET) plus @ConditionalOnClass(DispatcherServlet.class) (L54-L57). Jackson and the message converters are configured the same way.

4. The server starts inside refresh. The web context overrides onRefresh() and calls createWebServer(), which looks up exactly one ServletWebServerFactory bean and asks it for a server (ServletWebServerApplicationContext.java L149-L157). The default port is 8080 unless server.port says otherwise.

Follow-up: to switch to Jetty, exclude spring-boot-starter-tomcat and add the Jetty starter. The Tomcat class condition then fails and the Jetty one matches. No code changes are needed.

Alternate interview scenarios

"How would you design a reusable Spring Boot starter for common security, logging, metrics and configuration?"

I build it the same way Spring Boot builds its own starters. An autoconfigure module holds @AutoConfiguration classes, listed in META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports. A thin starter module only pulls in that module and its dependencies. Every bean it defines is guarded with conditions such as @ConditionalOnClass, @ConditionalOnMissingBean and @ConditionalOnProperty, so an application can override or switch off any part. Settings live in a prefixed @ConfigurationProperties class with defaults and generated metadata. For security I contribute a default SecurityFilterChain that backs off when the application defines its own. I test it with ApplicationContextRunner across classpath and property combinations, and I version it like a public API.

Sources (3)
  1. Spring Boot reference: Startersdocs.spring.io
  2. Spring Boot reference: Embedded servlet container supportdocs.spring.io
  3. Spring Boot: Creating your own auto-configuration and starterdocs.spring.io
Previous
Next