HomeHow-ToHow To Create Spring Boot Console Application

How To Create Spring Boot Console Application

In this quick journal entry, we’ll explore how to create Spring Boot Console App. It will be a very simple console-based application.

Spring Initializr - Create a Sample Spring Boot Project Template
Spring Initializr – Create a Sample Spring Boot Project Template

We have used the default settings which are already populated by the Spring Initializr and downloaded the same. Unzip the project and using any of your favourite Java editor open this project. We are using the Intellij Idea editor as our choice for Java projects.

Maven Dependencies

Once the project is opened in the editor, let’s first checkout the POM.XML for the maven dependencies. For your reference, below is the sample file which you can match with your downloaded file.

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.3.1.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.example</groupId>
	<artifactId>demo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>demo</name>
	<description>Demo project for Spring Boot</description>
	<properties>
		<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>
			<exclusions>
				<exclusion>
					<groupId>org.junit.vintage</groupId>
					<artifactId>junit-vintage-engine</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
	</dependencies>
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
</project>

If you have a close look at this file, you will see the below entries which specifies that this project is a Spring Boot Starter project.

.
.
.
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.3.1.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
.
.
.
<dependencies>
    <dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter</artifactId>
    </dependency>
.
.
.

Console Application

Our console application consists of a single class: DemoApplication.java – this is the main class for out Spring Boot console application.

This class also implements Spring’s CommandLineRunner interfaceCommandLineRunner is a simple Spring Boot interface which provides us with a run method. Spring Boot will automatically call the run method of all beans implementing this interface after the application context has been loaded.

Here is the code snippet of our console application:

package com.example.demo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication implements CommandLineRunner {
	private static Logger LOG = LoggerFactory.getLogger(DemoApplication.class);
	public static void main(String[] args) {
		LOG.info("STARTING THE SPRING BOOT APPLICATION");
		SpringApplication.run(DemoApplication.class, args);
		LOG.info("APPLICATION FINISHED IT'S WORKING");
	}
	@Override
	public void run(String... args) throws Exception {
		System.out.println("Welcome to your Spring Boot Console Application");
	}
}

When you try to execute this application, we will see the following log entries.

17:00:57.941 [main] INFO com.example.demo.DemoApplication - STARTING THE SPRING BOOT APPLICATION
2020-07-06 17:00:58.973  INFO 83377 --- [main] com.example.demo.DemoApplication: Welcome to your Spring Boot Console Application
2020-07-06 17:00:58.973  INFO 83377 --- [main] com.example.demo.DemoApplication: APPLICATION FINISHED IT'S WORKING

You must have noticed that the run method is called after application context is loaded but before the execution of the main method is complete.

Most console applications will only have a single class that implements CommandLineRunner. If your application has multiple classes that implement CommandLineRunner, the order of execution can be specified using Spring’s @Order annotation.

Conclusion

In this journal entry, we’ve tried to summarize how we can create a simple console-based application using Spring Boot.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular