To create a Spring Boot console application, the main class must implement the CommandLineRunner
interface and override the run()
method.
Dependencies
We only need the spring-boot-starter
dependency for the console application. Here is what our build.gradle
file looks like:
build.gradle
plugins {
id 'org.springframework.boot' version '2.1.3.RELEASE'
id 'java'
}
apply plugin: 'io.spring.dependency-management'
group = 'com.attacomsian'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter'
}
Note: We used the Gradle plugin version
5.2.1
for this example. We used theimplementation
keyword in the dependencies list instead ofcompile
.compile
configuration was depreciated starting from Gradle version3.0
.
If you are using Maven, please add the following dependency to your pom.xml
file:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
Spring Classes
Let's create a simple Service
class that returns a message. It has an overloaded getMessage()
method that returns the default message if no argument is passed. The default message is an external property declared in the application.properties
file.
HelloService.java
package com.attacomsian.console.services;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
@Service
public class HelloService {
@Value("${message.default}")
private String message;
public String getMessage() {
return message;
}
public String getMessage(String message) {
return "Hey, " + message;
}
}
application.properties
message.default=attacomsian.com
Now create the main class for our Spring Boot console application which implements the CommandLineRunner
interface. This interface provides a simple run()
method, which Spring Boot automatically invokes after application context has been loaded.
ConsoleApplication.java
package com.attacomsian.console;
import com.attacomsian.console.services.HelloService;
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;
@SpringBootApplication
public class ConsoleApplication implements CommandLineRunner {
private HelloService helloService;
public ConsoleApplication(HelloService helloService) {
this.helloService = helloService;
}
public static void main(String[] args) {
SpringApplication app = new SpringApplication(ConsoleApplication.class);
// disable spring banner
app.setBannerMode(Banner.Mode.OFF);
app.run(args);
}
@Override
public void run(String... args) throws Exception {
//check if user passes any argument
if (args.length > 0) {
System.out.println(helloService.getMessage(args[0]));
} else {
//print the default message
System.out.println(helloService.getMessage());
}
}
}
In the code above, we use the @SpringBootApplication
annotation on our main class to enable auto-configuration. The run()
method is the entry point of our application. Inside this method, we first check if the user passes any command-line argument or not. If the argument is availabile, we call the corresponding HelloService
method.
Another way to explicitly inform Spring Boot that this is not a web application is by using external properties. For example, we can specify the following properties in our application.properties
file to disable the Spring Boot banner on startup. It will also not start the application as an embedded web server.
spring.main.web-application-type=none
spring.main.banner-mode=off
Execution
Execute the following command in your terminal to run the Gradle project:
$ cd go/to/your/project/directory
$ ./gradlew bootRun
...
attacomsian.com
If you want to pass a command-line argument, use the --args
flag.
$ ./gradlew bootRun --args "Mike"
...
Hey, Mike
Source code: Download the complete source code from GitHub available under MIT license.
Conclusion
That's all for creating a console (non-web) application in Spring Boot. We discussed several options to inform Spring Boot that this is not a web application.
Most of the time, console applications are simple and have only one class that implements CommandLineRunner
. But if you want to implement the CommandLineRunner
interface more than once, you should use the @Order
annotation to specify their execution sequence.
Read Next: How to scaffold a Spring Boot application
✌️ Like this article? Follow me on Twitter and LinkedIn. You can also subscribe to RSS Feed.