Skip to content

Make it easier to use Spring Shell without Spring Boot #1207

@fmbenhassine

Description

@fmbenhassine

As of v3.4.x, there is no documentation on how to use Spring Shell without Spring Boot. Moreover, the configuration of shell runners is quite laborious:

import org.jline.reader.LineReader;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.shell.core.Shell;
import org.springframework.shell.core.command.annotation.Command;
import org.springframework.shell.core.command.annotation.EnableCommand;
import org.springframework.shell.core.command.annotation.Option;
import org.springframework.shell.core.context.ShellContext;
import org.springframework.shell.core.jline.InteractiveShellRunner;
import org.springframework.shell.core.jline.PromptProvider;

@EnableCommand(SpringShellApplication.class)
public class SpringShellApplication {

	public static void main(String[] args) throws Exception {
		ApplicationContext context = new AnnotationConfigApplicationContext(SpringShellApplication.class);
		InteractiveShellRunner shellRunner = context.getBean(InteractiveShellRunner.class);
		shellRunner.run(args);
	}

	@Command
	public String hello(@Option(defaultValue = "World") String name) {
		return "Hello " + name + "!";
	}

	// TODO define collaborators as beans to be injected in InteractiveShellRunner: not so easy..

	@Bean
	public InteractiveShellRunner interactiveApplicationRunner(LineReader lineReader, PromptProvider promptProvider,
															   Shell shell, ShellContext shellContext) {
		return new InteractiveShellRunner(lineReader, promptProvider, shell, shellContext);
	}

}

What would be even better, is that the user is not required to do that, even without Spring Boot:

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.shell.core.command.annotation.Command;
import org.springframework.shell.core.command.annotation.EnableCommand;
import org.springframework.shell.core.command.annotation.Option;
import org.springframework.shell.core.jline.InteractiveShellRunner;

@EnableCommand(SpringShellApplication.class)
public class SpringShellApplication {

	public static void main(String[] args) throws Exception {
		ApplicationContext context = new AnnotationConfigApplicationContext(SpringShellApplication.class);
        // choose which shell runner to use (interactive, non interactive or script) and launch it
        // runners (and their collaborators) should be pre-configured by EnableCommand and ready to be used here
		InteractiveShellRunner shellRunner = context.getBean(InteractiveShellRunner.class);
		shellRunner.run(args);
	}

	@Command
	public String hello(@Option(defaultValue = "World") String name) {
		return "Hello " + name + "!";
	}

}

In this case (similar to other projects like Batch, Integration, etc), EnableCommand would import a ImportBeanDefinitionRegistrar that defines shell runners and their collaborators as beans in the context ready to be used (and possibly customized) by Spring Shell users.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions