Spring Shell :

In this article, we are going to explain to you about another spring feature called Spring Shell.

Not all apps require a stylish interface sometimes, to interact with a terminal use application is the most convenient way to perform commands.

Spring Shell allows the individual to easily create such a workable application. In this case, the user can enter and execute their commands. 

Advantages of Spring Shell:

  1. Define a variety of commands with only one annotation.
  2. Using Spring Boot Auto-Configuring Feature
  3. Run script
  4. Customization of commands, handle results and errors
  5. Enable or disable dynamic commands based on existing criteria
  6. Ability to validate APIs

To use this tool, we need to add the following depencency to the program.

<dependency>
    <groupId>org.springframework.shell</groupId>
    <artifactId>spring-shell-starter</artifactId>
    <version>2.0.1.RELEASE</version>
</dependency>

Then we put @ShellComponent in our class.

@ShellComponent
public class MyCommands {
}

As you can see in the code below, we enter our command with @ShellMethod settings.

@ShellMethod(value = "Add numbers.", key = "sum")
public int add(int a, int b) {
  return a + b;
}

In the sample code, two numbers are received as inputs and their sum is displayed as output.

Run the program and enter the command we placed as a key in our function settings: Then the result is displayed as follows: You can get the full version of the code through our GitHub.