SpringTechnology

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">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.amitph.springboot</groupId>
    <artifactId>spring-boot-command-line</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-boot-command-line</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</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>

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 {
	mavenCentral()
}

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.

@Service
public class NonWebService {
    public void printMessage(String[] arguments) {
        System.out.println("Inside NonWebService Class. Received below arguments");
        Arrays.stream(arguments).forEach(System.out::println);
    }
}

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.

@SpringBootApplication
public class Application implements CommandLineRunner {

    @Autowired NonWebService service;

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


    @Override
    public void run(String... args) throws Exception {
        service.printMessage(args);
    }
}

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
This
is
first
Sprign
Boot
Non-Web
Application

Summary

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.


Leave a Reply

Your email address will not be published. Required fields are marked *