Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

November Discount Launch 2022 – Top
We’re finally running a Black Friday launch. All Courses are 30% off until next Friday:


November Discount Launch 2022 – TEMP TOP (NPI)
We’re finally running a Black Friday launch. All Courses are 30% off until next Friday:


Expanded Audience – Frontegg – Security (partner)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

1. Introduction

Spring Boot is a great framework for quickly creating new Java applications for a variety of use cases. One of the most popular uses is as a web server, using one of the many supported embedded servlet containers and template engines.

However, Spring Boot has a number of uses that do not require a web server: console applications, job scheduling, batch or stream processing, serverless applications, and more.

In this tutorial, we'll look at several different ways to use Spring Boot without a web server.

2. Using Dependencies

The easiest way to prevent a Spring Boot application from starting an embedded web server is to not include the web server starter in our dependencies.

This means not including the spring-boot-starter-web dependency in either the Maven POM or Gradle build file. Instead, we would want to use the more basic spring-boot-starter dependency in its place.

Keep in mind it's possible for Tomcat dependencies to be included in our application as transitive dependencies. In this case, we might need to exclude the Tomcat library from whichever dependency is including it.

3. Modifying the Spring Application

Another way to disable the embedded web server in Spring Boot is by using code. We can use either the SpringApplicationBuilder:

new SpringApplicationBuilder(MainApplication.class)

Or we can use the reference to the SpringApplication:

SpringApplication application = new SpringApplication(MainApplication.class);

In either case, we have the benefit of keeping the servlet and container APIs available on the classpath. This means we can still use the web server libraries without starting the web server. This can be useful, for example, if we want to use them to write tests or use their APIs in our own code.

4. Using Application Properties

Using code to disable the web server is a static option — it will affect our application no matter where we deploy it. But what if we want to create the web server in specific circumstances?

In this case, we can use Spring application properties:


Or using the equivalent YAML:

    web-application-type: none

The benefit of this approach is we can conditionally enable the web server. Using Spring profiles or conditionals, we can control the web server behavior in different deployments.

For example, we could have the web server running in development only to expose metrics or other Spring endpoints while keeping it disabled in production for security reasons.

Note that some earlier versions of Spring Boot used a boolean property named web-environment to enable and disable the web server. With the adoption of both traditional and reactive containers in Spring Boot, the property has been renamed and now uses an enum.

5. Conclusion

There are many reasons for creating Spring Boot applications without a web server. In this tutorial, we've seen multiple ways to do this. Each has its own pros and cons, so we should pick the approach that best meets our needs.

November Discount Launch 2022 – Bottom
We’re finally running a Black Friday launch. All Courses are 30% off until next Friday:


Generic footer banner
Comments are closed on this article!