Standalone Application Using Spring Boot

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

 

Introduction

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="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>
   <groupId>com.javadevjournal</groupId>
   <artifactId>standalone-application</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <packaging>jar</packaging>
   <name>standalone-application</name>
   <description>Spring Boot Standalone application</description>
   <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>2.0.0.RELEASE</version>
      <relativePath />
      <!-- lookup parent from repository -->
   </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>
         </plugin>
      </plugins>
   </build>
</project>

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 {

    @Override
    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.

@SpringBootApplication
public class SpringBootStandaloneApplication implements CommandLineRunner {

	public static void main(String[] args) {
		SpringApplication.run(SpringBootStandaloneApplication.class, args);
	}
	@Bean
	public HelloService getHelloService(){
		return  new DefaultHelloService();
	}

	@Override
	public void run(String... args) throws Exception {
		getHelloService().hello();
	}
}

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] ------------------------------------------------------------------------
[INFO] Building standalone-application 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
INFO] 
[INFO] Results:
[INFO] 
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] 
[INFO] 
[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] 
[INFO] --- spring-boot-maven-plugin:2.0.0.RELEASE:repackage (default) @ standalone-application ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[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

 

Summary

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.