Codersee

Spring Boot Console App With Kotlin

1. Introduction

Hello! In this, short step-by-step guide, I will show you how to create a Spring Boot console app with Kotlin from scratch.

In this article, we will learn:

  • ✅ What is the CommandLineRunner and how can it help us create a Spring Boot console app?
  • ✅ How to implement an example Spring Boot console app with Kotlin

2. What is CommandLineRunner?

As the first step, let’s take a minute to understand what exactly CommandLineRunner is. As the documentation states:

Interface used to indicate that a bean should run when it is contained within a SpringApplication.

In simple terms, the CommanLineRunner is an interface, which contains one method- void run(String... args) throws Exception, which we can implement and which will be invoked automatically, without us having to do it manually.

Moreover, we can implement it multiple times and control the order of invocation using the @Order annotation or the Ordered interface.

3. Create Project From Scratch

With that being said, let’s generate a new Spring Boot Kotlin application. To do so, let’s navigate to the Spring Initializr page and specify the following settings:

The screenshot shows Spring Initializr page and settings necessary for this Spring Boot console app project.

Basically, I would just like to emphasize that we don’t need any additional dependencies and the rest is totally up to you (and that I’m using the particular versions in case anything is deprecated in the future).

And once you pick your desired settings, please hit the Generate button and import the project to your IDE.

4. Implement CommandLineRunner

Finally, let’s open up our IDE and implement two classes called WillBeInvokedFirst and WillBeInvokedSecond:

@Component
@Order(1)
class WillBeInvokedFirst : CommandLineRunner {

  override fun run(vararg args: String) {
    print("Hello, ")
  }
}

@Component
@Order(2)
class WillBeInvokedSecond : CommandLineRunner {

  override fun run(vararg args: String) {
    println("World!")
  }
}

As we can see, we have to do only three things to implement a command-line application:

  • firstly, override the run method
  • secondly, annotate our classes with @Component annotation- to mark them as candidates for auto-detection
  • thirdly, specify the @Order annotation- this way we explicitly control, which run implementation is invoked first (lower value = higher priority)

 

Image shows two ebooks people can get for free after joining newsletter

 

Besides, no other changes are required. Nevertheless, please keep in mind that by default, the CommandlineApplication.kt file was added when creating a project:

@SpringBootApplication
class CommandlineApplication

fun main(args: Array<String>) {
  runApplication<CommandlineApplication>(*args)
}

Finally, if we run the application we should see the following output:

Hello, World!

5. Summary

And that would be all for this short article on how to create a Spring Boot console app with Kotlin. If you’d like to clone the project, then you can find it in this GitHub repository.

Let me know in the comments section if you enjoy such short articles solving particular problems. Of course as an addition to standard, long tutorials.

Finally, if you enjoyed this one, then you might be interested in other Spring Boot articles I created so far.

Leave a Reply

Your email address will not be published. Required fields are marked *

Categories

Author

Hi there! 👋

Hi there! 👋

My name is Piotr and I've created Codersee to share my knowledge about Kotlin, Spring Framework, and other related topics through practical, step-by-step guides. Always eager to chat and exchange knowledge.

Master Backend With Kotlin

Subscribe To My Newsletter

Get 3 Free Ebooks and weekly dose of backend knowledge

To make Codersee work, we log user data. By using our site, you agree to our Privacy Policy and Terms of Use.