How Spring Boot Detects Whether Your App Is a Web Application

This article explains how Spring Boot determines the type of an application—NONE, SERVLET, or REACTIVE—by inspecting classpath indicator classes through the WebApplicationType enum, ClassUtils.isPresent, and forName methods, complete with code examples and step‑by‑step logic.

Senior Brother's Insights
Senior Brother's Insights
Senior Brother's Insights
How Spring Boot Detects Whether Your App Is a Web Application

WebApplicationType Enum

Spring Boot uses the WebApplicationType enum to represent the three possible application modes: NONE (no embedded web server), SERVLET (standard servlet‑based web app), and REACTIVE (reactive web app).

How the type is deduced

When a SpringApplication instance is created, its constructor assigns

this.webApplicationType = WebApplicationType.deduceFromClasspath();

. The static deduceFromClasspath() method checks the classpath for indicator classes using ClassUtils.isPresent() and returns the appropriate enum constant.

this.webApplicationType = WebApplicationType.deduceFromClasspath();

The deduceFromClasspath() implementation (simplified) follows these steps:

If WEBFLUX_INDICATOR_CLASS is present and neither WEBMVC_INDICATOR_CLASS nor JERSEY_INDICATOR_CLASS are present, return REACTIVE.

Iterate over SERVLET_INDICATOR_CLASSES; if any class is missing, return NONE.

Otherwise return SERVLET.

The helper method

ClassUtils.isPresent(String className, ClassLoader classLoader)

simply tries to load the class with ClassUtils.forName(className, classLoader). If loading succeeds it returns true; if a ClassNotFoundException or other error occurs it returns false.

public static boolean isPresent(String className, @Nullable ClassLoader classLoader) {
    try {
        forName(className, classLoader);
        return true;
    } catch (Throwable ex) {
        return false;
    }
}

The underlying forName method obtains a suitable ClassLoader, attempts to load the target class, and if that fails, tries to load an inner‑class version before re‑throwing the exception.

public static Class<?> forName(String name, @Nullable ClassLoader classLoader) throws ClassNotFoundException, LinkageError {
    // obtain class loader, try direct load, then try inner class if necessary
    // (implementation details omitted for brevity)
}

The enum defines the concrete indicator class names that drive the detection logic:

private static final String[] SERVLET_INDICATOR_CLASSES = {
    "javax.servlet.Servlet",
    "org.springframework.web.context.ConfigurableWebApplicationContext"
};
private static final String WEBMVC_INDICATOR_CLASS = "org.springframework.web.servlet.DispatcherServlet";
private static final String WEBFLUX_INDICATOR_CLASS = "org.springframework.web.reactive.DispatcherHandler";
private static final String JERSEY_INDICATOR_CLASS = "org.glassfish.jersey.servlet.ServletContainer";

Putting everything together, Spring Boot determines the application type by:

Creating SpringApplication and invoking deduceFromClasspath().

Using ClassUtils.isPresent() to check for the presence of the indicator classes.

Returning REACTIVE if only the reactive indicator is present, NONE if servlet indicators are missing, and SERVLET otherwise.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendJavaSpringBootApplicationDetectionWebApplicationType
Senior Brother's Insights
Written by

Senior Brother's Insights

A public account focused on workplace, career growth, team management, and self-improvement. The author is the writer of books including 'SpringBoot Technology Insider' and 'Drools 8 Rule Engine: Core Technology and Practice'.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.