2016-09-28 152 views
1

這是如何在spring引導遠程shell中將參數和選項傳遞給自定義遠程shell命令?

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

按照上面的問題我想春天啓動遠程shell建議的後續問題。

我創建了我的自定義命令本文檔以下,

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

但在現實世界中使用的情況下,我們主要有參數和選項命令。文檔中沒有提及如何定義它們,以便用戶在運行命令時應該通過。

這是很好的解釋和容易做的彈殼。

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

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

但是,我們不能使用彈簧外殼與春天開機。

這種用例的可靠生產就緒解決方案是什麼?

更新:

上了車崩潰文檔解決方案

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

我洛成殼後看到mycommand的在命令的列表,但是當我運行命令,

但我得到例外

"Could not create command commandName instance". 

我正嘗試在命令中使用spring beans,所以我沒有自動連接,例如

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; 

    } 

} 

回答

3

我不認爲你可以在遠程shell命令中注入bean。

您可以但是注入InvocationContext到你的方法,並用它來檢索彈簧從上下文託管bean:

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

一個完整的例子,其工作對我來說:

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 
+0

謝謝,是啊我試過這個,但是沒有一個Java命令使用beanfactory來獲取bean的例子。不知道什麼是正確的語法。在這裏使用groovy比java更有優勢嗎? – vishal

+0

BeanFactory beans = context.getAttributes()。get(「spring.beanfactory」); \t \t MyService myService = beans.getBean(MyService.class); – vishal

+0

答案更新 – alexbt