Spring Boot - Creating a standalone application example

Posted on July 29, 2017


Spring Boot provides the various “Starters” for building standalone java application based on Spring framework. In this post we will use the spring-boot-starter for creating a standalone application which prints “Hello Spring Boot”.

Tools and technologies used for this application are-

  • Spring Boot 1.5.4.RELEASE
  • JavaSE 1.8
  • Maven 3.3.9
  • Eclipse Neon.3

Project Structure 

Review the spring boot project structure.

spring-boot-standalone.png

Jar dependencies

To create a standalone application in spring boot, you need to add the spring-boot-starter dependency in your pom.xml file. 

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

Here is the complete pom.xml file.

pom.xml

<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.boraji.tutorial.springboot</groupId>
  <artifactId>spring-boot-standalone-example</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <properties>
    <java.version>1.8</java.version>
  </properties>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.4.RELEASE</version>
  </parent>
  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter</artifactId>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>
</project>

Spring Bean class

Create a HelloWorldBean class under com.boraji.tutorial.springboot.bean package as follows.

HelloWorldBean.java

package com.boraji.tutorial.springboot.bean;

import org.springframework.stereotype.Component;

@Component
public class HelloWorldBean {
   public void sayHello(){
      System.out.println("Hello Spring Boot!");
   }
}

Main class

To bootstrap spring boot application, create a main class annotated with @SpringBootApplication annotation.

MainApp.java

package com.boraji.tutorial.springboot;

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

import com.boraji.tutorial.springboot.bean.HelloWorldBean;

@SpringBootApplication
public class MainApp {
   public static void main(String[] args) {
      ApplicationContext ctx = SpringApplication.run(MainApp.class, args);

      HelloWorldBean bean = ctx.getBean(HelloWorldBean.class);
      bean.sayHello();
   }
}

 

Run application

Run the MainApp.java class as Java application i.e. go to Run→ Run as → Java Application

You can use the mvn spring-boot:run command to run the spring boot application.

After executing the main class, the console output will look like as follow.

spring-boot-standalone1.png

Download Sources