如何在spring boot remote shell中将参数和选项传递给自定义远程shell命令? [英] How to pass arguments and options to custom remote shell command in spring boot remote shell?

查看:254
本文介绍了如何在spring boot remote shell中将参数和选项传递给自定义远程shell命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是关于



如何使用spring shell在Spring启动Web应用程序中构建控制台命令?



根据以上问题的建议我正在尝试spring boot remote shell。



我已根据此文档创建了自定义命令,



http:// docs.spring.io/spring-boot/docs/current/reference/html/production-ready-remote-shell.html#production-ready-extending-the-remote-shell



但在现实世界的用例中,我们主要有参数和选项来命令。在doc中没有提到如何定义它们以便用户在运行命令时应该通过。



这在Spring-shell中很容易理解和解释。 / p>

http://docs.spring.io/spring-shell/docs/current/reference/htmlsingle/#simple-application



https://projects.spring.io/spring-shell/



但我们不能在弹簧靴上使用弹簧外壳。



这些用例的可靠生产就绪解决方案是什么?



更新:



在CRaSH doc上获得解决方案



http://www.crashub.org/ 1.3 / reference.html#developping_commands



我可以在登录shell之后在命令列表中看到myCommand,但是当我运行命令时,



但我得到异常

 无法创建命令commandName实例。 

我试图在命令中使用spring bean,所以我做了自动装配,例如

 包命令; 

import org.crsh.cli.Argument;
import org.crsh.cli.Command;
import org.crsh.cli.Usage;
import org.crsh.cli.Option;
import org.crsh.cli.Required;
import org.crsh.command.BaseCommand;
import org.crsh.command.InvocationContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;


@Usage(我的测试计算命令。)
公共类myCommand扩展BaseCommand {

protected final Logger log = LoggerFactory.getLogger(的getClass());

/ **
*
* /
public Service1 service1;

/ **
*
* /
公共服务2服务2;

/ **
* @param service1
* @param service2
* /
@Autowired
public myCommand(Service1 service1,Service2 service2){
super();
this.service1 = service1;
this.service2 = service2;
}


@Usage(测试命令)
@Command
公共地图< String,Double> command1(InvocationContext< Object> context,@ Usage(id)@ Required @Argument int id)
throws Exception {


Map< String,Double> result = new HashMap<>();

result.put(key1,service1.compute(id));
result.put(key2,service2.compute(id));

返回结果;

}

}


解决方案

我认为你不能在远程shell命令中注入bean。



但是你可以在你的方法中注入一个InvocationContext并用它来从您的上下文中检索spring托管bean:

  @Usage('使用spring.beanfactory示例')
@Command
def mycommand(InvocationContext context,...){
BeanFactory beans = context.attributes ['spring.beanfactory']
YourBean bean = beans.getBean(YourBean.class);
...
}

一个适合我的完整示例:

 包命令

import org.crsh.cli.Command
import org.crsh。 cli.Usage
import org.crsh.command.InvocationContext
import org.springframework.beans.factory.BeanFactory
import com.alexbt.goodies.MyBean

class SayMessage {
字符串消息;
SayMessage(){
this.message =你好;
}

@Usage(默认命令)
@Command
def main(InvocationContext context,@ Usage(A Parameter)@ Option(names = [p,param])String param){
BeanFactory beanFactory =(BeanFactory)context.getAttributes()。get(spring.beanfactory);
MyBean bean = beanFactory.getBean(MyBean.class);
返回消息++ bean.getValue()++ param;
}

@Usage(Hi子命令)
@Command
def hi(InvocationContext context,@ Usage(A Parameter)@ Option(names = [p,param])String param){
BeanFactory beanFactory =(BeanFactory)context.getAttributes()。get(spring.beanfactory);
MyBean bean = beanFactory.getBean(MyBean.class);
返回Hi+ bean.getValue()++ param;
}
}

> saymsg -p Johnny
>您好,我的朋友Johnny

> saymsg hi -p Johnny
>我的朋友Johnny


This is followup question on

how to build console command in spring boot web application using spring shell?

As per suggestions on above question I am trying spring boot remote shell.

I have created my custom command following this documentation,

http://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-remote-shell.html#production-ready-extending-the-remote-shell

But in real world use cases, we mostly have arguments and options to command. There is no mention in doc about how to define them so that user should pass while running commands.

This is very well explained and easy to do in spring-shell.

http://docs.spring.io/spring-shell/docs/current/reference/htmlsingle/#simple-application

https://projects.spring.io/spring-shell/

But we cant use spring shell with spring boot.

what are the reliable production ready solutions to such use cases ?

Update:

Got the solution on CRaSH doc

http://www.crashub.org/1.3/reference.html#developping_commands

I can see myCommand in the list of commands after loggin into shell, but when I run command,

but I am getting exception

"Could not create command commandName instance".

I am trying to use spring beans in command so I did autowiring e.g.

package commands;

import org.crsh.cli.Argument;
import org.crsh.cli.Command;
import org.crsh.cli.Usage;
import org.crsh.cli.Option;
import org.crsh.cli.Required;
import org.crsh.command.BaseCommand;
import org.crsh.command.InvocationContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;


@Usage("My test computation commands.")
public class myCommand extends BaseCommand {

    protected final Logger log = LoggerFactory.getLogger(getClass());

    /**
     * 
     */
    public Service1 service1;

    /**
     * 
     */
    public Service2 service2;

    /**
     * @param service1
     * @param service2
     */
    @Autowired
    public myCommand(Service1 service1, Service2 service2) {
        super();
        this.service1 = service1;
        this.service2 = service2;
    }


    @Usage("Test command")
    @Command
    public Map<String, Double> command1(InvocationContext<Object> context, @Usage("id") @Required @Argument int id)
            throws Exception {


        Map<String, Double> result = new HashMap<>();

        result.put("key1", service1.compute(id));
        result.put("key2", service2.compute(id));

        return result;

    }

}

解决方案

I don't think you can inject beans in your Remote shell command.

You may however inject an InvocationContext into your method and use it to retrieve a spring managed bean from your context:

@Usage('Example using spring.beanfactory')
@Command
def mycommand(InvocationContext context, ...) {
    BeanFactory beans = context.attributes['spring.beanfactory']
    YourBean bean = beans.getBean(YourBean.class);
    ...
}

A Complete example which works for me:

package commands

import org.crsh.cli.Command
import org.crsh.cli.Usage
import org.crsh.command.InvocationContext
import org.springframework.beans.factory.BeanFactory
import com.alexbt.goodies.MyBean

class SayMessage {
    String message;
    SayMessage(){
        this.message = "Hello";
    }

    @Usage("Default command")
    @Command
    def main(InvocationContext context, @Usage("A Parameter") @Option(names=["p","param"]) String param) {
        BeanFactory beanFactory = (BeanFactory) context.getAttributes().get("spring.beanfactory");
        MyBean bean = beanFactory.getBean(MyBean.class);
        return message + " " + bean.getValue() + " " + param;
    }

    @Usage("Hi subcommand")
    @Command
    def hi(InvocationContext context, @Usage("A Parameter") @Option(names=["p","param"]) String param) {
        BeanFactory beanFactory = (BeanFactory) context.getAttributes().get("spring.beanfactory");
        MyBean bean = beanFactory.getBean(MyBean.class);
        return "Hi " + bean.getValue() + " " + param;
    }
}

> saymsg -p Johnny
> Hello my friend Johnny

> saymsg hi -p Johnny
> Hi my friend Johnny

这篇关于如何在spring boot remote shell中将参数和选项传递给自定义远程shell命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆