The Spring Boot command line application example walks you through the process of creating a non-web application example spring boot step by step. Spring Boot is sub-project developed by developers of spring framework, Spring Boot makes it easy to create stand-alone, production-grade Spring based applications with minimum configuration possible.
Other interesting posts you may like
Let’s begin:
Now, we will create Spring Boot command line application example step by step
Project Structure
A standard Maven project structure.
Maven Dependency
In this Spring Boot command line application example, we only need the spring-boot-starter
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.javabycode</groupId> <artifactId>spring-boot-application-example</artifactId> <packaging>jar</packaging> <version>1.0</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.2.RELEASE</version> <relativePath/> </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> |
Create Spring service
We create a service to return a message.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package com.javabycode.service; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; @Service public class HelloService { @Value("${name:unknown}") private String name; public String getMessage() { return getMessage(name); } public String getMessage(String name) { return "Hello " + name; } } |
and a resource property file which located in class path, application.properties:
1 |
msg=Javabycode |
Create Spring Boot Application
We create Spring Boot Application class which implements CommandLineRunner and has the main method
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
package com.javabycode; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.Banner; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import com.javabycode.service.HelloService; import static java.lang.System.exit; @SpringBootApplication public class MyConsoleApplication implements CommandLineRunner { @Autowired private HelloService helloService; public static void main(String[] args) throws Exception { SpringApplication app = new SpringApplication(MyConsoleApplication.class); app.setBannerMode(Banner.Mode.OFF); app.run(args); SpringApplication.run(MyConsoleApplication.class, args); } @Override public void run(String... args) throws Exception { if (args.length > 0 ) { System.out.println(helloService.getMessage(args[0].toString())); }else{ System.out.println(helloService.getMessage()); } exit(0); } } |
Dig deeper:
a. As you see above, in case you don’t want to see the spring logo you can disable it with line code:
1 |
app.setBannerMode(Banner.Mode.OFF); |
b. The run method will be the entry point when you run the Spring Boot application
Run the Spring Boot Application
Foremost, we need to go to project directory in console, then package and run it using command line.
1 2 3 4 5 6 7 |
mvn package java -jar target/spring-boot-application-example-1.0.jar Hello Javabycode java -jar target/spring-boot-application-example-1.0.jar "Spring Boot Application" Hello Spring Boot Application |
That’s all on the Spring Boot command line application example.
References
CommandLineRunner JavaDoc
Download complete source code, click link below
spring-boot-application-example.zip (415 downloads)