How to Write a non-web Application with Spring Boot

A quick guide on How to write your own Spring Boot non-web application within minutes.

Overview

The Spring Framework provides an application with Spring’s DI, IoC and several other useful features. On top of that, Spring Boot brings in auto-configuration, dependency version management, active profiles, etc. We often use Spring and Spring Boot frameworks for building web-based applications. However, we can also build non-web applications, aka standalone applications using Spring Boot.

The Java standalone applications bootstrap from the “static void main(..)” method and finish at the end of the method. Using Spring Boot to create non-web applications, we can leverage the framework’s several helpful features.

This tutorial provides a step-by-step guide to building a Spring Boot non-web application.

Maven Dependencies

Like any other Spring Boot projects, we use ‘spring-boot-starter-parent‘ as a parent in the Maven pom.xml.

Our pom.xml looks straightforward.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>{spring-boot-version}></version>
    <relativePath/>
  </parent>

  <groupId>com.amitph.springboot</groupId>
  <artifactId>spring-boot-command-line</artifactId>
  <version>1.0</version>
  <name>spring-boot-command-line</name>
  <description>Demo project for Spring Boot</description>

  <properties>
    <java.version>{java.version}</java.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter</artifactId>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>
</project>Code language: HTML, XML (xml)

Gradle Dependencies

Else, if you like working with Gradle, use the below build.gradle file.

plugins {
  id 'org.springframework.boot' version '{spring.boot.version}'
  id 'java'
}

apply plugin: 'io.spring.dependency-management'

group = 'com.amitph.springboot'
version = '1.0'
sourceCompatibility = '{java.version}'

repositories {
  mavenCentral()
}

dependencies {
  implementation 'org.springframework.boot:spring-boot-starter'
  testImplementation 'org.springframework.boot:spring-boot-starter-test'
}Code language: Gradle (gradle)

Service Class

Let’s create a Service component for our application. This service class is responsible for performing application tasks. Thanks to Spring, we can use one of Spring’s stereotype annotations to make this class a Spring bean and auto-wire it in the @SpringBootApplication class.

@Service
public class NonWebService {
  public void printMessage(String[] args) {
    System.out.println("Started with arguments: ");
    Arrays.stream(args).forEach(System.out::println);
  }
}Code language: Java (java)

Application Class

Lastly, we will create the Application class. All Spring Boot applications bootstrap from the main() method in the @SpringBootApplication class.

Our Application class implements the CommandLineRunner interface, one of the Spring Boot Runners, and overrides its run() method. When we launch the Application, Spring Boot starts the Spring context, loads the required beans and their dependencies and invokes the run method with the original arguments to the main() method.

We invoke our service method from the run() method.

@SpringBootApplication
public class Application 
    implements CommandLineRunner {

  @Autowired 
  private NonWebService service;

  public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
  }

  @Override
  public void run(String... args) throws Exception {
    service.printMessage(args);
  }
}Code language: Java (java)

We are leveraging Spring Framework’s auto-wiring support to inject dependencies.

Run the Application

Let’s launch our Spring Boot-based non-web application. There are several ways to run a Spring Boot application, for example, running the JAR from the command line, using Maven Spring Boot goal, or using an IDE.

We launch this application by executing the JAR from the command line and passing a few arguments.

$ java -jar spring-boot-command-line.jar \
    My first Spring Boot Non-Web ApplicationCode language: Bash (bash)

Once successfully started, we should see the application’s service method run with all the arguments we provided.

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.3.RELEASE)

INFO [main] com.amitph.spring.nonweb.Application     : Starting Application v0.0.1-SNAPSHOT on Amits-office-mac.local with PID 86488 (/Users/aphaltankar/Workspace/personal/spring-boot-command-line/target/spring-boot-command-line.jar started by aphaltankar in /Users/aphaltankar/Workspace/personal/spring-boot-command-line/target)
INFO [main] com.amitph.spring.nonweb.Application     : No active profile set, falling back to default profiles: default
INFO [main] com.amitph.spring.nonweb.Application     : Started Application in 0.884 seconds (JVM running for 1.278)
Started with arguments: 
My
first
Spring
Boot
Non-Web
ApplicationCode language: plaintext (plaintext)

Summary

This step-by-step guide taught us to create a Spring Boot non-web Application. Although A Spring Boot non-web application, also known as a standalone application, doesn’t need the web component, it can leverage auto-configuration, auto-wiring, DI and IoC and several other features of Spring and Spring Boot.

We created the Application class of the CommandLineRunner type and a service class to perform application-level tasks. We launched the application with a few arguments and successfully printed the command line arguments from the service method.

You can refer to our GitHub Repository for the complete source code of the examples used in this tutorial.


4 thoughts on “How to Write a non-web Application with Spring Boot

  1. Thank you for writing this tutorial. I never used Spring before, so I was wondering under what circumstances I’d want to develop desktop application using Spring.

    1. Thanks for reaching out Quazi. Regarding your question, a simple answer is ‘it depends’
      First of all, Spring Doesn’t provide any framework or abstractions over the basic desktop operations. This point decides a lot.
      However, you can still use Spring for your Component initializations and wirings keeping the components physically loose coupled.

  2. Thanks for the tutorial, I was wondering how it was possible to solve the inverse situation. I have an old java executable (without spring, spring-boot) used as desktop app, and I would like to add the web layer in oder to call it with an entrypoint from another app (for example to have a start/stop)

    1. Mark, you can easily build a web application, e.g. a Spring Boot Service. For executing the executable you can use something like ProcessBuilder.
      Then, you can trigger the executable from an HTTP endpoint of your web application or from a JMS message.

Comments are closed.