Create a Standalone Spring Boot Application with ApplicationRunner

Hello Friends!, today we are going to learn a step-by-step process to create a standalone Spring Boot application without spring MVC.

we can create a Spring Boot standalone application using CommandLineRunner or ApplicationRunner Interface.

in this post we are going to use ApplicationRunner Interface.

please check our post Create a Standalone Spring Boot Application with CommandLineRunner

Create a Spring Boot Application

There are multiple ways to create Spring Boot application.

Using Spring Initializr.

Application in STS.

Application with Eclipse and Maven manually

Add Spring Boot Starter dependency in Maven.

add spring-boot-starter dependency in pom.xml file. it is the only dependency we required to create Spring Boot standalone application.

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

our application pom.xml file as follow.

<?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 https://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.3.0.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.gyanideveloper.springboot</groupId>
	<artifactId>spring-boot-standalone</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>spring-boot-standalone</name>
	<description>Spring Boot stand-alone project</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>
			<exclusions>
				<exclusion>
					<groupId>org.junit.vintage</groupId>
					<artifactId>junit-vintage-engine</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

Create Service Class

create HelloService interface and declare sayHello(-) method in it as follow.

package com.gyanideveloper.springboot.service;

public interface HelloService {
	public String sayHello(String name);
}

implement the HelloService interface and override the sayHello(-) as follow.

package com.gyanideveloper.springboot.service;

import org.springframework.stereotype.Service;

@Service
public class HelloServiceImpl implements HelloService {

	@Override
	public String sayHello(String name) {
		return "Hello " + name + ", welcome to Gyanideveloper.com";
	}

}

Implement the ApplicationRunner Interface

Example implementation of ApplicationRunner interface as follow.

you can implement ApplicationRunner interface as follow, run() gives access to the ApplicationArguments.

autowire the HelloService instance and invoke sayHello(-) method as shown in example.

package com.gyanideveloper.springboot.command;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import com.gyanideveloper.springboot.service.HelloService;

@Component
public class CustApplicationRunner implements ApplicationRunner {

	@Autowired
	private HelloService helloService;

	private static final Logger logger = LoggerFactory.getLogger(CustApplicationRunner.class);

	@Override
	public void run(ApplicationArguments args) throws Exception {
		logger.info(helloService.sayHello("Gyani Developer"));
	}

}

Create SpringBootApplication main class

create class App and add main(-) method as shown in follow Example.

package com.gyanideveloper.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class App {

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

}

Run Application.

running the Spring Boot application, it is just the same as a Java Program. we need to run the main method or We can also run the application by executing mvn spring-boot:run command.


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

2020-06-14 19:44:58.153  INFO 3936 --- [           main] com.gyanideveloper.springboot.App        : Starting App on DESKTOP-R5VMSGQ with PID 3936 (F:\workspace-spring-tool-suite-4-4.6.1.RELEASE\spring-boot-standalone\target\classes started by Sada in F:\workspace-spring-tool-suite-4-4.6.1.RELEASE\spring-boot-standalone)
2020-06-14 19:44:58.159  INFO 3936 --- [           main] com.gyanideveloper.springboot.App        : No active profile set, falling back to default profiles: default
2020-06-14 19:44:59.236  INFO 3936 --- [           main] com.gyanideveloper.springboot.App        : Started App in 2.26 seconds (JVM running for 4.577)
2020-06-14 19:44:59.239  INFO 3936 --- [           main] c.g.s.command.CustApplicationRunner      : Hello Gyani Developer, welcome to Gyanideveloper.com

Why use the CommandLineRunner interface.

ApplicationRunner interface is not mandatory to create a Spring Boot standalone application, but we get some advantages with it.

ApplicationRunner executed just before the application startup complete.

it gets executed after the application context loaded so we could use it to check if certain beans exist or required values correctly initialized.

it gives access to the raw String array of incoming main method arguments.

Final Note

we learn how Create a Standalone Spring Boot Application using the ApplicationRunner interface in detail. full example code is available on GitHub

Leave a Comment