Yes, Spring Boot can be used to build non-web applications. Although it is widely known for developing web applications and REST APIs, it also supports standalone applications such as batch jobs, command-line tools, schedulers, data-processing programs, messaging consumers, and microservice workers. In a non-web application, Spring Boot starts the Spring container without launching an embedded web server.
Key Points: • Spring Boot is not limited to web applications; it can be used for standalone and background-processing applications. • Non-web applications do not require embedded servers such as Tomcat, Jetty, or Undertow. • Features like Dependency Injection, Scheduling, Spring Batch, and Messaging can still be used.
Example: Suppose a company needs a nightly job that reads data from a file, processes it, and stores the results in a database. This can be implemented as a Spring Boot non-web application running on a schedule without exposing any REST endpoints.
Code Example:
@SpringBootApplication
public class DataProcessorApplication
implements CommandLineRunner {
public static void main(
String[] args) {
SpringApplication.run(
DataProcessorApplication.class,
args);
}
@Override
public void run(String... args)
throws Exception {
System.out.println(
"Processing data...");
}
}Disable Web Server:
application.properties
spring.main.web-application-type=noneCommon Non-Web Use Cases:
• Batch Processing Applications • Data Migration Tools • File Processing Systems • Scheduled Jobs • Message Queue Consumers • ETL Applications • Background Worker Services • Command-Line Utilities
Runner Interfaces:
1. CommandLineRunner
• Executes after the Spring context starts. • Receives command-line arguments.
2. ApplicationRunner
• Similar to CommandLineRunner. • Provides a richer API for handling arguments.
Example:
@Component
public class StartupRunner
implements ApplicationRunner {
@Override
public void run(
ApplicationArguments args) {
System.out.println(
"Application Started");
}
}Benefits:
• Lightweight and easy to deploy. • Reuses Spring Boot features such as DI and configuration management. • Supports scheduling, messaging, and batch processing. • Suitable for automation and background tasks.
Real-World Example:
A banking system may use a Spring Boot non-web application to:
• Read transaction files every night. • Validate records. • Generate reports. • Send notifications.
No web server is required because the application runs as a background process.
Interview Tip: A concise interview answer is: Yes, Spring Boot can be used to build non-web applications such as batch jobs, schedulers, command-line tools, and message consumers. By setting spring.main.web-application-type=none and using CommandLineRunner or ApplicationRunner, Spring Boot starts the application without launching an embedded web server while still providing all core Spring features.