Spring Shell :

In the Mirbozorgi team, we are going to talk to you about the features of spring in this article.  One of the features of spring that is very popular in today’s society is the spring shell.

Note that not all applications need to have a beautiful user interface. In some cases, having an application terminal is the best way to execute commands and interact with an application.

Spring Shell allows people to easily run such programs. In this case, the user can enter his commands and execute them.

Advantages of Spring Shell:

  1. Define different types of commands with just one annotation
  2. Use the automatic spring boot configuration feature
  3. Executive script
  4. Handle results and errors and order commands
  5. Possibility to deactivate or activate dynamic commands based on existing criteria
  6. Ability to validate IPs

You must add the following dependency to the application to use this tool

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

Then you need to put @ShellComponent in your class

@ShellComponent
public class MyCommands {
}

As you can see in the image below, you can enter your command settings using @ShellMethod

@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 input and the sum of those two numbers is displayed as output.

Run the program and enter the command code that you entered as the key in your function settings.

The results are then displayed to you as follows:

You can download the full version of the code through our GitHub.

Leave A Comment

+ 4 = 9

Please Send Email

Your message sent successfully
There has been an error