Explain Codes LogoExplain Codes Logo

Spring Boot without the web server

java
spring-boot
web-server
dependency-exclusions
Alex KataevbyAlex Kataev·Nov 9, 2024
TLDR

To launch a Spring Boot app without a web server, modify your pom.xml to omit web starters:

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

Configure SpringApplication to not spin-up a server:

import org.springframework.boot.WebApplicationType; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; @SpringBootApplication public class Application { public static void main(String[] args) { new SpringApplicationBuilder(Application.class) .web(WebApplicationType.NONE) // It's like Iron-Man without his suit .run(args); } }

Through this streamlined setup, you create a Spring Boot application, excluding the web context auto-configuration.

Delving deeper: Configuration & Pitfalls

Choice of web application type

Don't forget with Spring Boot, you can select either a REACTIVE or SERVLET type for web servers. However, to completely disable these, set spring.main.web-application-type=NONE in properties or WebApplicationType.NONE in code.

Dependency Exclusions

Remember, while excluding web-server dependencies is key to avoiding their auto-configuration, ensure to examine each dependency you use thoroughly, especially those that may implicitly include a web server.

Conditional Class Loading

In non-web applications, ensure to leverage the @ConditionalOnClass annotation - it helps to avoid loading unnecessary beans, thereby reducing memory overhead.

Post-Startup Tasks

Planning to run a few tasks post startup? Remember to implement CommandLineRunner or ApplicationRunner, handy for scheduled tasks or data processing.

Application Version

Spring Boot 2.x got you covered with the above method. But, Spring Boot 1.x users, you have to use spring.main.web-environment=false to disable the web server.

JMS and Logging

Ensure to configure JMS and logging maintaining non-web environment to prevent unforeseen issues or exceptions.

Looking Ahead

While you may not need a web server now, design your application for future scaling. You never know when you might need to scale-up to include web capabilities.

Beyond the basics

Common Hurdles

Beware of the common tripwires - forgetting about health checks and actuator endpoints. Find a suitable workaround, like exposing JMX endpoints or building custom health checks.

Architecture Concerns

In a microservice ecosystem, even non-web services need to be detectable. Keep in mind to use a service registry for seamless interactions.

The Pros of Not Being Web-Based

One of the key advantages of not having a web server is, you've axed the overhead associated with managing web layers. Your application is now leaner, faster, and more efficient, which is golden, especially in data processing and integration jobs.

Testing Non-Web Apps

Testing non-web applications can be tricky. A good practice is to rely heavily on Mocking frameworks and context loads to create a comprehensive test suite.