Spring Boot - Creating a standalone application example

Spring Boot - Creating a standalone application example

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 - Creating a standalone application example | BORAJI.COM


Jar dependencies

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


Here is the complete pom.xml file.


<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">


Spring Bean class

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


package com.boraji.tutorial.springboot.bean;

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


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;

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

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


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 - Creating a standalone application example | BORAJI.COM