MTI TEK
Spring Framework
|
Spring Shell
Spring Shell
Overview
spring-shell-starter is versioned independently from Spring Boot — it must be declared with an explicit <version> since spring-boot-starter-parent does not manage Spring Shell's version in its BOM. Spring Shell 4.x requires Spring Boot 4+.
- Spring Shell 4 ships alongside Spring Boot 4 and Spring Framework 7. The legacy annotations
@ShellComponent, @ShellMethod, and @ShellOption from org.springframework.shell.standard — deprecated in v3 — have been completely removed in v4.
- Spring Shell autoconfigures the full interactive REPL (built-in commands:
help, clear, script, version, quit/exit) with zero additional configuration. When running under Spring Boot, command scanning is activated automatically — no @EnableCommand or @CommandScan is needed on the application class.
Maven Dependency
<properties>
<spring-shell.version>4.0.2</spring-shell.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.shell</groupId>
<artifactId>spring-shell-starter</artifactId>
<version>${spring-shell.version}</version>
</dependency>
</dependencies>
- In Spring Shell 4,
spring-shell-standard and spring-shell-standard-commands have been merged into spring-shell-core. The starter handles all transitive dependencies.
- The starter now imports
spring-boot-starter itself — a separate spring-boot-starter dependency is redundant when using Spring Shell 4.
Defining Shell Commands: @Command and @Option
- All annotations live in
org.springframework.shell.core.command.annotation.
@Command is placed on methods to mark them as shell commands. The declaring class must be a Spring-managed bean (@Component). There is no replacement for @ShellComponent — it was always a @Component variant.
- The command name is set explicitly via the
name attribute — there is no automatic camelCase-to-kebab-case derivation from the method name. The description attribute replaces the value string from @ShellMethod.
- Method parameters require
@Option for binding — unlike @ShellMethod which used implicit positional arguments. Options are passed on the command line as --longName value or -s value for short names.
- The return value behavior is unchanged: returning a non-void type prints the value automatically; a
void method must write output explicitly (e.g., System.out.println).
package com.mtitek.spring.shell;
import org.springframework.shell.core.command.annotation.Command;
import org.springframework.shell.core.command.annotation.Option;
import org.springframework.stereotype.Component;
@Component
public class ShellCommands {
@Command(name = "min-value", description = "Display Min Value")
public int minValue() {
return 1_000;
}
@Command(name = "max-value", description = "Display Max Value")
public void maxValue() {
System.out.println(1_000_000);
}
@Command(name = "to-lower-case", description = "Convert to lower case")
public String toLowerCase(
@Option(longName = "input", shortName = 'i', description = "String to convert", defaultValue = "HELLO")
String input) {
return input.toLowerCase();
}
@Command(name = "add-values", description = "Add two values")
public Integer addValues(@Option(shortName = 'a') Integer a, @Option(shortName = 'b') Integer b) {
return a + b;
}
}
@Option(longName = "input", shortName = 'i', defaultValue = "HELLO") — defaultValue makes the option optional; omitting it from the command line uses the declared default.
@Option(shortName = 'a') without longName — the option is available only as -a; no --a long form is registered.
Running the Shell
shell:> help
AVAILABLE COMMANDS
Built-In Commands
clear: Clear the terminal screen
help: Show help about available commands
quit, exit: Exit the shell
script: Execute commands from a script file
version: Show version info
Shell Commands
add-values: Add two values
max-value: Display Max Value
min-value: Display Min Value
to-lower-case: Convert to lower case
shell:> min-value
1000
shell:> max-value
1000000
shell:> to-lower-case --input ABC
abc
shell:> add-values -a 5 -b 7
12
Notes
- Built-in commands in v4 are reduced compared to v3 —
history and stacktrace are no longer listed.
- Options and arguments can be specified in any order. Both
--longName=value and --longName value forms are accepted, as well as -s=value and -s value for short names.
- Spring Shell's interactive loop blocks
SpringApplication.run() until quit or exit is typed. For non-TTY/CI execution, set spring.shell.interactive.enabled=false in application.properties.