How to run spring boot as a standalone application (non-web)?
Till now we have given examples on spring boot running as a web based application. In this page we are giving
an example on how to create spring boot standalone application. In spring boot, we need to implement CommandLineRunner interface
and override run() method to create standalone application.
Since we are creating standalone application, include only "spring-boot-starter" in the 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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.java2novice.springboot</groupId>
<artifactId>spring-boot-tutorials</artifactId>
<packaging>jar</packaging>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<!-- Package as an executable jar/war -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
|
Here is our starting class. Since it implements CommandLineRunner interface, run() method would be the
entry point for the code.
package com.java2novice.springboot;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootStandaloneApplication implements CommandLineRunner{
public static void main(String[] args) throws Exception {
SpringApplication app = new SpringApplication(SpringBootStandaloneApplication.class);
app.run(args);
}
@Override
public void run(String... arg0) throws Exception {
// TODO Auto-generated method stub
System.out.println("Spring boot standalone application is working...");
}
}
|
|