Create a Standalone Spring Boot Application.

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 CommandLineRunner Interface.

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 CommandLineRunner Interface

Example implementation of CommandLineRunner interface as follow.

you can implement CommandLineRunner interface as follow, run() gives access to the raw String array of incoming main method arguments.

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.CommandLineRunner;
import org.springframework.stereotype.Component;

import com.gyanideveloper.springboot.service.HelloService;

@Component
public class CommandRunner implements CommandLineRunner {

	@Autowired
	private HelloService helloService;

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

	@Override
	public void run(String... 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-11 00:23:28.021  INFO 9144 --- [           main] com.gyanideveloper.springboot.App        : Starting App on DESKTOP-R5VMSGQ with PID 9144 (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-11 00:23:28.025  INFO 9144 --- [           main] com.gyanideveloper.springboot.App        : No active profile set, falling back to default profiles: default
2020-06-11 00:23:28.658  INFO 9144 --- [           main] com.gyanideveloper.springboot.App        : Started App in 1.001 seconds (JVM running for 2.376)
2020-06-11 00:23:28.661  INFO 9144 --- [           main] c.g.springboot.command.CommandRunner     : Hello Gyani Developer, welcome to Gyanideveloper.com

Why use the CommandLineRunner interface.

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

CommandLineRunner 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 CommandLineRunner interface in detail. full example code is available on GitHub

1 thought on “Create a Standalone Spring Boot Application.”

Leave a Comment