Want to make a Spring Boot console app? Want to learn how to use Spring's CommandLineRunner to replicate the type of simple testing you would ordinarily have done in Java's main method?
In this quick Spring Boot console app tutorial, we will create a simple application that not only does dependency injection, but also hits an H2 database with the help of a SpringBoot JDBC Data CrudRepository interface.
If you want to learn how to create a simple Spring Boot Console App, this example is for you!
Now here's some AI generated content stuffing to help capture some keywords and get some visibility on this video.
What is CommandLineRunner?
CommandLineRunner is a functional interface in Spring Boot that allows you to run a piece of code when the application starts. It is typically used for tasks such as initializing data, running scripts, or performing startup checks.
Comparison with Java Main Method
Purpose:
Java Main Method: Entry point for any standalone Java application. It is the first method that gets executed.
CommandLineRunner: Executes after the Spring Boot application has started and the context has been initialized. It is used for running specific pieces of code at startup.
Context:
Java Main Method: Runs outside of any framework and doesn’t have access to Spring’s dependency injection and other features.
CommandLineRunner: Runs within the Spring context, allowing full access to Spring’s features like dependency injection, bean lifecycle, and configuration.
Execution Timing:
Java Main Method: Runs immediately when the application starts.
CommandLineRunner: Runs after the Spring Boot application context has been fully set up.
When to Use CommandLineRunner?
Use CommandLineRunner when you need to execute specific logic once the Spring Boot application has started and the application context is fully available. This can include tasks like loading initial data into the database, starting background threads, or logging startup information.
Spring Shell vs. Spring Boot Console Application
Spring Shell:
Spring Shell is a library that helps create interactive command-line applications. It provides an interactive shell with commands that can be executed by the user.
Features:
Supports tab completion, history, and built-in commands.
Easy to define custom commands.
Suitable for creating CLI tools with interactive capabilities.
Example Use Case:
An interactive CLI tool for managing a web application, where users can type commands to perform various administrative tasks.
Spring Boot Console Application:
A Spring Boot console application is a non-web application that runs from the command line and uses Spring Boot features like dependency injection, configuration management, and more.
Features:
Executes a specific piece of code at startup, often without user interaction.
Uses CommandLineRunner or ApplicationRunner for startup logic.
Example Use Case:
A batch processing job that runs at application startup, processes data, and then exits.
Key Differences:
Interaction:
Spring Shell: Designed for interactive use, where users can type and execute commands.
Spring Boot Console App: Typically non-interactive, executing predefined logic and then terminating.
Use Case:
Spring Shell: Ideal for creating tools that require user input and interaction.
Spring Boot Console App: Ideal for running scripts, batch jobs, or any startup logic that doesn’t require user interaction during execution.
In summary, CommandLineRunner is used within Spring Boot to execute startup logic within the Spring context, providing access to Spring features. Spring Shell is used for creating interactive command-line applications, while a Spring Boot console application is typically non-interactive and runs predefined startup logic.