How does Spring Boot decide which embedded server to use if multiple options are available in the classpath?

Spring Boot automatically selects an embedded web server based on the server implementation available on the application's classpath. By default, Tomcat is chosen because it is included in the spring-boot-starter-web dependency. If multiple server implementations are present, Spring Boot follows its auto-configuration rules and the dependency configuration to determine which server should be initialized.

Key Points: • Tomcat is the default embedded server in Spring Boot. • Spring Boot selects a server based on the dependencies available on the classpath. • Only one embedded server is typically activated at runtime. • Developers can replace Tomcat with Jetty or Undertow by changing dependencies. • Server selection is handled through Spring Boot's auto-configuration mechanism.

Default Behavior

When we add:

spring-boot-starter-web

Spring Boot automatically includes:

• Embedded Tomcat • Spring MVC • Jackson • Validation libraries

As a result, Tomcat becomes the default server.

Example:

spring-boot-starter-web | Embedded Tomcat Added | Tomcat Starts | Application Ready

How Spring Boot Chooses the Server

During startup:

Application Starts | Classpath Scanning | Server Dependencies Detected | Auto-Configuration Applied | Selected Server Starts

Spring Boot checks which server implementation libraries are available and applies the corresponding auto-configuration.

Common Embedded Servers

• Tomcat (Default) • Jetty • Undertow

These servers can be used interchangeably depending on project requirements.

Using Jetty Instead of Tomcat

Code Example:

<dependency>
    <groupId>
        org.springframework.boot
    </groupId>
    <artifactId>
        spring-boot-starter-web
    </artifactId>

    <exclusions>
        <exclusion>
            <groupId>
                org.springframework.boot
            </groupId>
            <artifactId>
                spring-boot-starter-tomcat
            </artifactId>
        </exclusion>
    </exclusions>
</dependency>

<dependency>
    <groupId>
        org.springframework.boot
    </groupId>
    <artifactId>
        spring-boot-starter-jetty
    </artifactId>
</dependency>

Now Spring Boot starts Jetty instead of Tomcat.

Using Undertow

Code Example:

<dependency>
    <groupId>
        org.springframework.boot
    </groupId>
    <artifactId>
        spring-boot-starter-undertow
    </artifactId>
</dependency>

Spring Boot will use Undertow when Tomcat is excluded.

What Happens if Multiple Servers Are Present?

Having multiple embedded server implementations on the classpath is not recommended.

Example:

• Tomcat • Jetty

both included together.

This can lead to:

• Auto-configuration conflicts • Ambiguous server selection • Unexpected startup behavior

Best practice is to keep only one embedded server dependency active.

Example: Suppose a project includes:

spring-boot-starter-web

Since this starter already contains Tomcat, Spring Boot automatically starts:

Embedded Tomcat

and exposes the application on:

http://localhost:8080

If Tomcat is removed and Jetty is added, Spring Boot automatically starts Jetty instead.

Benefits of Embedded Server Auto-Configuration

• Minimal setup • Faster development • Easy server replacement • Consistent deployment model • Simplified configuration

Real-World Example

A company develops multiple microservices:

• User Service • Product Service • Order Service

Most services use the default Tomcat server.

For high-performance workloads, one service may switch to Undertow simply by replacing the dependency, without changing application code.

Interview Tip: A concise interview answer is:

"Spring Boot selects the embedded server based on the server libraries available on the classpath. By default, spring-boot-starter-web includes Tomcat, so Tomcat is used automatically. If Tomcat is excluded and Jetty or Undertow is added, Spring Boot's auto-configuration mechanism detects the available server and starts it instead."