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. Similar to Spring Boot web application a Spring Boot Stand Alone or non-web applications are also pretty easy.
Learn with the help of real world Spring Boot Application.

This tutorial is source code oriented so you will directly go through writing a Spring Boot non-web Application.

Maven Dependencies

If you use Maven, refer to the below pom.xml.

<?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">
        <relativePath/> <!-- lookup parent from repository -->
    <description>Demo project for Spring Boot</description>





Gradle Dependencies

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

plugins {
	id 'org.springframework.boot' version '2.1.3.RELEASE'
	id 'java'

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

group = 'com.amitph.spring'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

repositories {

dependencies {
	implementation 'org.springframework.boot:spring-boot-starter'
	testImplementation 'org.springframework.boot:spring-boot-starter-test'

Service Class

Let’s write a Service component of the application. Just like any web applications, a Spring Boot Stand Alone application may have a Service Layer. Note that it is annotated with @Service just like any other Spring bean.

public class NonWebService {
    public void printMessage(String[] arguments) {
        System.out.println("Inside NonWebService Class. Received below arguments");

Application Class

Finally, it is a time to write the Application. Unlike the web applications, in non-web applications we have to implement CommandLineRunner interface. Hence, we have to provide implementation for its abstract run method. This method is a main method for our application as it the starting point for the app level logic. Moreover the the run method also passes all the command line arguments.

public class Application implements CommandLineRunner {

    @Autowired NonWebService service;

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

    public void run(String... args) throws Exception {

Thing to Note: We have Autowired the service class just like any other Spring Autowiring.

Run the Application

In your favourite IDE you can simply right click your Application and click run. However, you can also run it from command line using application jar file. Firstly, generate your application jar file using Maven or Gradle build. After this you can run the command, pass argument and see the service prints it.

$ java -jar spring-boot-command-line.jar This is first Sprign Boot Non-Web Application

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

2019-03-05 08:54:00.202  INFO 86488 --- [           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)
2019-03-05 08:54:00.205  INFO 86488 --- [           main] com.amitph.spring.nonweb.Application     : No active profile set, falling back to default profiles: default
2019-03-05 08:54:00.756  INFO 86488 --- [           main] com.amitph.spring.nonweb.Application     : Started Application in 0.884 seconds (JVM running for 1.278)
Inside NonWebService Class. Received below arguments


In this quick, tutorial you learnt How to Create Spring Boot Non-Web Application in minutes. The application can also be called as Spring Boot Standalone application.

To develop a non web application, your Application needs to implement CommandLineRunner interface along with its run method. This run method acts like a main of your application.

The Full Source Code of the Tutorials used in this article can be found at Github.

2 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.

Comments are closed.