Spring Boot Console Application
To create a console application (non-web application), Main class needs to implement CommandLineRunner and override the run method.
Follow the below steps to create the application.
1. Maven Project Structure
2. Dependencies / Starters
we need spring-boot-starter only.
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 43 44 45 46 47 48 49 50 |
<?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.harinathk</groupId> <artifactId>SpringBootConsole</artifactId> <version>1.0</version> <packaging>jar</packaging> <name>SpringBootConsole</name> <description>Spring Boot Console Application</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.4.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> |
3. Spring Classes
Here, we are adding pojo class to return the message.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
package com.harinathk.service; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; @Service public class HelloService { @Value ("${message:unknown}") private String message; public String getMessage() { return getMessage(message); } public String getMessage(String message) { return "Hello " + message; } } |
1 |
message=harinathk.com |
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 |
package com.harinathk; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import com.harinathk.service.HelloService; @SpringBootApplication public class SpringBootConsoleApplication implements CommandLineRunner{ @Autowired HelloService helloService; public static void main(String[] args) throws Exception { SpringApplication app = new SpringApplication(SpringBootConsoleApplication.class); app.run(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()); } System.exit(0); } } |
This is the main class which implements ComamndLineRunner, run method will be the entry point.
4. Run it
package it and run.
1 2 3 4 5 6 7 |
$ mvn package $ java -jar target/SpringBootConsole-1.0.jar Hello harinathk.com $ java -jar target/SpringBootConsole-1.0.jar "New String Message" Hello New String Message |
Thank you.