Spring Boot
Auto-configuration
Spring Web
Java
Configuration Management

How to prevent spring-boot autoconfiguration for spring-web?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Spring Boot auto-configuration is useful until it starts creating web infrastructure you do not want. If spring-web is on the classpath, Boot may auto-configure MVC-related beans and treat the application as a web app. The right way to prevent that depends on what you actually mean: disable the whole web application mode, or exclude only the specific web auto-configuration classes.

Exclude Specific Auto-Configuration Classes

If you still want a Spring Boot application but do not want MVC auto-configuration, exclude the relevant classes directly.

java
1import org.springframework.boot.SpringApplication;
2import org.springframework.boot.autoconfigure.SpringBootApplication;
3import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;
4
5@SpringBootApplication(exclude = {WebMvcAutoConfiguration.class})
6public class DemoApplication {
7    public static void main(String[] args) {
8        SpringApplication.run(DemoApplication.class, args);
9    }
10}

This is the most targeted option when you know exactly which Boot web auto-configuration you want to suppress.

You can also configure exclusions in properties:

properties
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration

That keeps the main application annotation smaller.

Disable Web Application Mode Entirely

If the project should not behave as a web application at all, the more direct choice is setting the web application type to none.

properties
spring.main.web-application-type=none

This is usually better than selectively excluding MVC if your application is really a command-line tool, batch job, or message-processing service that happens to have spring-web on the classpath transitively.

Know What You Are Actually Excluding

The phrase "prevent Spring Boot autoconfiguration for spring-web" is slightly imprecise. spring-web the library is not one single auto-configuration toggle. Boot responds to classpath conditions and may activate several related configurations, depending on what else is present.

That is why the safe approach is:

  1. inspect which auto-configurations are being applied
  2. exclude the specific ones you do not want
  3. verify the resulting application type

For debugging, the condition evaluation report is helpful:

properties
debug=true

That makes Boot print which auto-configurations matched and why.

Watch for Transitive Dependencies

Sometimes spring-web is not declared directly. It arrives transitively through another starter, and then Boot behaves like a web application unexpectedly. When that happens, the right fix may be dependency cleanup rather than annotation-level exclusions.

Use the dependency tree when the source of the behavior is unclear:

bash
mvn dependency:tree

That helps distinguish "Boot configured something because I asked for it" from "Boot configured something because another starter pulled it in".

Prefer Exclusions Over Fighting Beans After the Fact

Sometimes developers leave auto-configuration enabled and then override or delete beans manually. That usually creates harder-to-maintain startup behavior. If Boot should not configure a subsystem, exclude it explicitly rather than working around it later.

This is especially true for servlet infrastructure such as DispatcherServlet, view resolution, and MVC defaults.

In other words, the clean fix is usually earlier in the startup chain than most first attempts. If Boot should not create the web stack, tell Boot that directly rather than patching around the created beans later.

Common Pitfalls

  • Excluding WebMvcAutoConfiguration when the real goal was disabling all web application behavior.
  • Leaving spring-web on the classpath and assuming Boot will ignore it automatically.
  • Fighting auto-configured beans manually instead of excluding the responsible configuration class.
  • Forgetting to inspect the condition report when exclusions do not behave as expected.
  • Removing useful web auto-configuration and then being surprised that MVC endpoints no longer work.

Summary

  • Preventing Spring Boot web auto-configuration starts with deciding whether you want partial or total web disablement.
  • Exclude WebMvcAutoConfiguration for targeted MVC suppression.
  • Use spring.main.web-application-type=none if the app should not be a web app at all.
  • Inspect Boot's condition report so exclusions are based on evidence, not guesswork.
  • Check transitive dependencies before assuming the problem is only annotation-level configuration.

Course illustration
Course illustration

All Rights Reserved.