如何无需Spring Boot运行Spring Shell 2?
Great question! Running Spring Shell without relying on Spring Boot is totally feasible—you just need to manually wire up the necessary Spring context and dependencies instead of leaning on Boot's auto-configuration magic. Let's walk through the exact steps to make this work:
1. Adjust Your Dependencies
First, remove any Spring Boot starters for Spring Shell (like spring-boot-starter-shell) and replace them with core Spring Shell libraries plus essential Spring Framework dependencies.
Maven Example
Add these to your pom.xml:
<dependencies> <!-- Core Spring Shell functionality --> <dependency> <groupId>org.springframework.shell</groupId> <artifactId>spring-shell-core</artifactId> <version>3.2.0</version> <!-- Use a version matching your Spring Framework version --> </dependency> <!-- For interactive terminal support (required for a usable shell) --> <dependency> <groupId>org.springframework.shell</groupId> <artifactId>spring-shell-jline</artifactId> <version>3.2.0</version> </dependency> <!-- Spring Core Context (required for bean management) --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>6.1.4</version> <!-- Match with Spring Shell's compatible Spring version --> </dependency> <!-- Spring Expression Language (used by Shell for command parsing) --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>6.1.4</version> </dependency> </dependencies>
Gradle Example
For build.gradle:
dependencies { implementation 'org.springframework.shell:spring-shell-core:3.2.0' implementation 'org.springframework.shell:spring-shell-jline:3.2.0' implementation 'org.springframework:spring-context:6.1.4' implementation 'org.springframework:spring-expression:6.1.4' }
Note: Always ensure version compatibility—Spring Shell 3.x requires Spring Framework 6.x and Java 17+, while Spring Shell 2.x works with Spring Framework 5.x and Java 8+.
2. Manually Configure the Spring Context
Instead of letting Spring Boot set up the context, create a configuration class that imports Spring Shell's built-in auto-configuration classes (to avoid manually registering every required bean) and scans your custom shell commands.
Here's a complete working example:
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import org.springframework.shell.Shell; import org.springframework.shell.autoconfigure.ShellAutoConfiguration; import org.springframework.shell.autoconfigure.jline.JLineShellAutoConfiguration; @Configuration // Import Spring Shell's auto-configs to set up core components like command parsers and terminal handlers @Import({ShellAutoConfiguration.class, JLineShellAutoConfiguration.class}) // Scan the package where your @ShellComponent classes are located @ComponentScan(basePackages = "com.yourproject.shell.commands") public class StandaloneShellApplication { public static void main(String[] args) { // Initialize the Spring context with our custom configuration AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(StandaloneShellApplication.class); // Retrieve the Shell bean from the context Shell shell = context.getBean(Shell.class); // Run the shell—this blocks until the user exits the terminal int exitCode = shell.run(args); // Ensure the JVM exits properly with the shell's exit code System.exit(exitCode); } }
3. Verify Your Custom Commands Work
Your existing @ShellComponent classes will work exactly as before, as long as they're in the package scanned by @ComponentScan. For example:
import org.springframework.shell.command.annotation.Command; import org.springframework.shell.command.annotation.Option; import org.springframework.shell.component.ShellComponent; @ShellComponent public class GreetingCommand { @Command(command = "greet", description = "Send a personalized greeting") public String greet(@Option(shortNames = 'n', description = "Your name") String name) { return "Hello, " + (name != null ? name : "Stranger") + "!"; } }
4. Run the Shell
Execute the main method in StandaloneShellApplication, and you'll get a fully functional interactive shell. Test your command by typing:
greet --name Stefan
You'll see the output: Hello, Stefan!
Key Additional Notes
- Non-interactive Mode: To run a single command and exit, pass the command as arguments when launching the app (e.g.,
java -jar your-app.jar greet --name Stefan). - Custom Bean Overrides: If you need to tweak any Shell-related beans (like command registration settings), you can override them in your
StandaloneShellApplicationconfiguration class just like in a regular Spring application.
内容的提问来源于stack exchange,提问作者Stefan




