Standalone Application Using Spring Boot

In this post, we will discuss creating a Spring Boot standalone application.



Spring Boot makes it easy to create stand-alone, production-grade Spring-based applications. Spring Boot provides various starters for building standalone or more traditional war deployments. We will create Spring Boot standalone application by implementing CommnadLineRunner interface.


1. Project Creation 

There are multiple ways to create a Spring Boot application.

We will use Spring initializr to create our project structure.

[pullquote align=”normal”]If you are starting with Spring Boot, read our article on building Spring Boot application. [/pullquote]


2. The Maven Dependency

The spring-boot-starter is the only required dependency to build our standalone application. Here is our pom.xml file

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="" xmlns:xsi="" xsi:schemaLocation="">
   <description>Spring Boot Standalone application</description>
      <relativePath />
      <!-- lookup parent from repository -->

Let’s discuss a few interesting points from the pom.xml file

  • Packaging for our application set to jar.
  • spring-boot-starter is the only required dependency.


3. Service Class

For this post, We are creating HelloService bean which will print greeting message to the calling API.

package com.javadevjournal.service;

public interface HelloService {
    void hello();

package com.javadevjournal.service;

public class DefaultHelloService implements HelloService {

    public void hello() {
       System.out.println("Hello from Hello Service");


4. Spring Boot Main Class

To bootstrap our application, let’s create our main class and annotate it with @SpringBootApplication annotation.

public class SpringBootStandaloneApplication implements CommandLineRunner {

	public static void main(String[] args) {, args);
	public HelloService getHelloService(){
		return  new DefaultHelloService();

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

Using CommandLineRunner is not required, but it provides several advantages during the application startup and management.

  • CommandLineRunner executes after the application context loaded so we could use it to check if certain beans exist or required values correctly initialized.
  • Provide control of the system startup process.


5. Run Application

Running Spring Boot application is like any other Java programme, we need to run the main method. We can also run the application by executing mvn spring-boot: run command.

5.1 Run Application

Spring Boot provides an option for creating a self-contained executable jar file to run in production. To create an executable jar, we need the spring-boot-maven-plugin in our pom.xml file (check section 2 for detail).Run mvn package command to create an executable jar.

$ mvn package
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building standalone-application 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] Results:
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] --- maven-jar-plugin:3.0.2:jar (default-jar) @ standalone-application ---
[INFO] Building jar: /deployment/standalone-application/target/standalone-application-0.0.1-SNAPSHOT.jar
[INFO] --- spring-boot-maven-plugin:2.0.0.RELEASE:repackage (default) @ standalone-application ---
[INFO] ------------------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 5.311 s
[INFO] Finished at: 2018-03-13T22:58:12-07:00
[INFO] Final Memory: 34M/306M
[INFO] ------------------------------------------------------------------------

On successful execution, we can find an executable jar (standalone-application-0.0.1-SNAPSHOT.jar) in the target directory.To run the application, use java -jar command

$ java -jar target/standalone-application-0.0.1-SNAPSHOT.jar

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

 2018-03-13 23:03:05.232  INFO 38482 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2018-03-13 23:03:05.246  INFO 38482 --- [           main] c.j.SpringBootStandaloneApplication      : Started SpringBootStandaloneApplication in 0.949 seconds (JVM running for 1.355)
Hello from Hello Service



Spring Boot provides several options to create a production-ready application. In this post, We covered building and running Spring Boot standalone application. Complete example for this post is available on the GitHub

4 thoughts on “Standalone Application Using Spring Boot”

    • Hi Kevin,

      Not sure if I got your question, are you saying why we need to put @Bean annotation when we have defined only one Class or are you referring to define bean in a separate configuration class? I need some context to answer your question.

  1. i got a probelm. // getHelloService().hello(); // tip me a error.
    i change // getHelloService() –> helloService // , then it’s ok. can you tell me why?

Comments are closed.