Spring Boot
web server
Java
microservices
standalone application

Spring Boot without the web server

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Spring Boot is a powerful framework for building standalone, production-grade Spring-based applications quickly and with minimal configuration. Although Spring Boot is frequently associated with web applications due to its embedded server capabilities, it can also be used for non-web applications, focusing purely on the core features of Spring. This article explores the use of Spring Boot without leveraging a web server, demonstrating its versatility and efficiency for non-web use cases.

Introduction to Spring Boot

Spring Boot, built on top of the Spring Framework, simplifies the development of Java applications by handling dependency management and offering a powerful set of application configurations out of the box. By eliminating boilerplate configurations, developers can focus on building business logic and delivering features rapidly.

Key Features of Spring Boot

Without a web server, Spring Boot still provides a comprehensive environment for developing robust applications. Key features that remain relevant include:

  1. Auto-Configuration: Based on the dependencies on your classpath, Spring Boot automatically configures the application context.
  2. Dependency Management: Handles libraries and transitive dependencies via a parent starter project.
  3. Many Starter Projects: Spring Boot simplifies project setup by providing starter POMs and configurations.
  4. Externalized Configuration: Supports external configurations, allowing for easy management and integration.
  5. Monitoring and Metrics: Even without a web layer, you can use Actuator for monitoring application components like beans and services.
  6. Banner Customization: Not necessarily a core feature, but Spring Boot allows for customizing the application startup banner.

Building a Non-Web Spring Boot Application

To begin with a non-web Spring Boot application, exclude web servers like Tomcat from your dependencies. Below is a typical pom.xml for such a project:

xml
1<project xmlns="http://maven.apache.org/POM/4.0.0"
2      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4    <modelVersion>4.0.0</modelVersion>
5
6    <groupId>com.example</groupId>
7    <artifactId>non-web-spring-boot</artifactId>
8    <version>1.0.0-SNAPSHOT</version>
9    <parent>
10        <groupId>org.springframework.boot</groupId>
11        <artifactId>spring-boot-starter-parent</artifactId>
12        <version>3.0.0</version>
13        <relativePath/> 
14    </parent>
15
16    <dependencies>
17        <!-- Core Spring Boot Dependencies -->
18        <dependency>
19            <groupId>org.springframework.boot</groupId>
20            <artifactId>spring-boot-starter</artifactId>
21        </dependency>
22        <!-- Other Non-Web Dependencies -->
23        <dependency>
24            <groupId>org.springframework.boot</groupId>
25            <artifactId>spring-boot-starter-data-jpa</artifactId>
26        </dependency>
27    </dependencies>
28
29    <dependencyManagement>
30        <dependencies>
31            <dependency>
32                <groupId>org.springframework.boot</groupId>
33                <artifactId>spring-boot-dependencies</artifactId>
34                <version>${spring-boot.version}</version>
35                <type>pom</type>
36                <scope>import</scope>
37            </dependency>
38        </dependencies>
39    </dependencyManagement>
40
41    <build>
42        <plugins>
43            <plugin>
44                <groupId>org.springframework.boot</groupId>
45                <artifactId>spring-boot-maven-plugin</artifactId>
46            </plugin>
47        </plugins>
48    </build>
49</project>

Example Application

Let’s consider a simple use case where a Spring Boot application interacts with a database, processes data, and logs the results.

java
1import org.springframework.boot.SpringApplication;
2import org.springframework.boot.autoconfigure.SpringBootApplication;
3import org.springframework.context.annotation.Bean;
4import org.springframework.context.annotation.ComponentScan;
5import org.springframework.scheduling.annotation.EnableScheduling;
6import org.springframework.scheduling.annotation.Scheduled;
7import org.slf4j.Logger;
8import org.slf4j.LoggerFactory;
9
10@SpringBootApplication
11@EnableScheduling
12public class NonWebApplication {
13
14    private static final Logger logger = LoggerFactory.getLogger(NonWebApplication.class);
15
16    public static void main(String[] args) {
17        SpringApplication.run(NonWebApplication.class, args);
18    }
19
20    @Bean
21    public CommandLineRunner demo() {
22        return args -> {
23            logger.info("Non-web application is processing data...");
24            // Implement data processing logic here
25        };
26    }
27
28    @Scheduled(fixedRate = 5000)
29    public void logStatus() {
30        logger.info("Scheduled task running without a web server...");
31    }
32}

Application Configuration

Spring Boot leverages the application.properties or application.yml files for configuration. Here’s an example of properties that focus on database connectivity without any web server configurations:

properties
1# Database Configuration
2spring.datasource.url=jdbc:h2:mem:testdb
3spring.datasource.driver-class-name=org.h2.Driver
4spring.datasource.username=sa
5spring.datasource.password=password
6
7# JPA Configuration
8spring.jpa.hibernate.ddl-auto=update
9spring.jpa.show-sql=true

Advantages of Spring Boot Without a Web Server

  1. Reduced Resource Use: Eliminating the web server creates a lighter application footprint, which makes your application suitable for small devices or microservice designs where a web layer isn't necessary.
  2. Faster Application Start: Without a web server to initialize, Spring Boot applications can start more quickly.
  3. Dedicated Task Applications: Ideal for batch processing, background tasks, or any service-oriented applications that don't require an HTTP interface.

Summary

While Spring Boot is predominantly recognized for making web applications easy to build, it can equally excel in applications devoid of a web layer. By focusing on its core strengths such as dependency management, externalized configurations, and monitoring, it provides an efficient and streamlined platform for non-web applications. Whether used for internal tools, batch processing, or standalone tasks, Spring Boot’s flexibility ensures broad applicability beyond its usual web-centric evolution.

Key Points Summary

Feature/ComponentDescription
Auto-ConfigurationAutomatically configures based on the classpath dependencies.
Dependency ManagementHandles libraries via parent starter project.
Externalized ConfigurationSupports configuration from outside your application for adaptability.
Monitoring with ActuatorSupplies insights and manageability even for non-web applications.
Non-Web Use CasesApplication suitable for tasks, CLI tools, and backend services.

Spring Boot's adaptability allows developers to harness the framework's extensive features across a broad range of applications, extending beyond web solutions into efficient and powerful non-web services.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.