Spring Boot Stand alone App Example

How to create Stand alone app?

With Spring Boot we can also create a stand-alone application, which does not need any web server or application server to run. In this article, we will go through a very simple Spring boot stand-alone application using the CommandLineRunner interface.
We will go through a very simple Spring Boot application trying to understand how CommandLineRunner works and what are the alternatives to CommandLineRunner interface like Application Runner. CommandLineRunner : This interface can be used to dictate that a class that implements this interface should be executed once the SpringApplication context loaded. This interface has a single method "run" which takes String array as arguments. ApplicationRunner : Similar to CommandLineRunner how it works, it also has only one method "run", which gets executed once the SpringBoot Context loads. The only difference between these two interfaces is argument types. the run method in ApplicationRunner accepts an array of ApplicationArguments whereas run method in CommandLineRunner accepts array or Strings.
CommandLineRunner.java
Definition of CommandLineRunner interface. Note that it is a Functional interface, meaning it has only one method.
@FunctionalInterface
public interface CommandLineRunner {
	/**
	 * Callback used to run the bean.
	 * @param args incoming main method arguments
	 * @throws Exception on error
	 */
	void run(String... args) throws Exception;
}

Technology Used

  • Java 11
  • Apache Maven 3.5.0
  • Spring Boot 2.2.6
  • Logback 1.2.3
  • Eclipse IDE

Code Structure


Application Details

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>
	<artifactId>bootng-springboot-hello</artifactId>
	<version>1.0.0</version>
	<packaging>jar</packaging>
	<name>Spring Boot Standalone App</name>
	<description>Spring Boot Hello Standalone Application</description>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.2.6.RELEASE</version>
		<relativePath />
	</parent>
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<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>
				<configuration>
					<addResources>true</addResources>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>
HelloService
Our service Class defines three methods hello, bye, and onDestroy marked with @PreDestroy annotation.
package com.bootng;
import javax.annotation.PreDestroy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

@Service
public class HelloService {
  private static final Logger log = LoggerFactory.getLogger(HelloService.class);
  public String hello() {
    return "Hello Spring Boot!";
  }
  @PreDestroy
  public void onDestroy() throws Exception {
    log.info("Spring Container is destroyed!");
    bye();
  }
  public String bye() {
    String msg = "Bye..... Have a nice day!";
    log.info(msg);
    return msg;
  }
}
HelloApplication Class
Our HelloApplication Class implements CommandLineRunner interface. We inject the HelloService using @Autowired annotation. In the run method, we call the service.hello method.
package com.bootng;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@ComponentScan({"com.bootng"})
@SpringBootApplication
public class HelloApplication implements CommandLineRunner {
  private static final Logger log = LoggerFactory.getLogger(HelloApplication.class);
  @Autowired
  HelloService service;
  @Override
  public void run(String... args) throws Exception {
    log.info(service.hello());
  }
}

Running the application

Run the application by executing the following command
mvn spring-boot:run
Console Output
20-June-01 16:28:57:549 INFO main c.b.HelloApplication:651 - No active profile set, falling back to default profiles: default 20-June-01 16:28:57:844 INFO main c.b.HelloApplication:61 - Started HelloApplication in 0.64 seconds (JVM running for 0.872) 20-June-01 16:28:57:845 INFO main c.b.HelloApplication:26 - Hello Spring Boot! 20-June-01 16:28:57:846 INFO SpringContextShutdownHook c.b.HelloService:21 - Spring Container is destroyed! 20-June-01 16:28:57:847 INFO SpringContextShutdownHook c.b.HelloService:27 - Bye..... Have a nice day!
We can see the application will execute and terminate with messages like "Hello Spring Boot!" and Bye..."Have a nice day!"
First message is printed by run->service.hello() method as soon as the application starts.
The second message is somewhat special and is printed by invoking service.bye() by the Spring framework before the application exits.
Git Source Code
  • git clone https://github.com/siddharthagit/spring-boot-references
  • cd spring-boot-standalone-app
  • mvn clean install
  • mvn spring-boot:run

    Summary

    Using CommandLineRunner we can create a stand alone Spring Boot application
    We can use the @PreDestroy to call methods from service class before it is unloaded by Spring while exiting the application

    No comments :

    Post a Comment

    Please leave your message queries or suggetions.