这是如何在spring引导远程shell中将参数和选项传递给自定义远程shell命令?
how to build console command in spring boot web application using spring shell?
按照上面的问题我想春天启动远程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;
}
}
谢谢,是啊我试过这个,但是没有一个Java命令使用beanfactory来获取bean的例子。不知道什么是正确的语法。在这里使用groovy比java更有优势吗? – vishal
BeanFactory beans = context.getAttributes()。get(“spring.beanfactory”); \t \t MyService myService = beans.getBean(MyService.class); – vishal
答案更新 – alexbt