我使用spring boot web starter创建了restfull web应用程序,效果很好.我可以通过网址访问它.
但是我需要创建可以在后端计算和存储一些值的控制台命令.我希望能够手动或通过bash脚本运行控制台命令.
我找不到任何关于如何在spring boot web应用程序中集成spring-shell项目的文档.
在spring boot starter https://start.spring.io/中也没有选择spring-shell依赖的选项
1)webapp和console需要是两个独立的应用程序吗?我需要单独部署它们吗?
2)是否可以在同一个应用程序中部署Web应用程序并运行控制台命令?
3)在shell和Web应用程序之间共享公共代码(模型,服务,实体,业务逻辑)的最佳方法是什么?
有人可以帮忙吗?
我正在使用IntelliJ中的CLI Spring shell代码.我运行它并给出一些参数.但是当我输入insert并按回车键时,控制台不接受它,看起来好像什么也没发生!
我的代码:
@Component
public class HelloWorldCommands implements CommandMarker {
@CliCommand(value = "insert", help = "insert data to ParsEMS DB")
public void insert() {
try {
Class.forName("org.postgresql.Driver");
Connection con = DriverManager.getConnection("jdbc:postgresql://localhost:5432/ParsEMS", "xxxxxx", "xxxxxxx");
Statement st = con.createStatement();
st.executeUpdate("INSERT INTO node (name, destination) VALUES ('b', 200)");
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("insert to ParsEMS DB");
}
}
public class Main {
public static void main(String[] args) throws Exception {
Bootstrap.main(args);
}
}
Run Code Online (Sandbox Code Playgroud)
我跑的时候会看到以下结果;
1.1.0.RELEASE
欢迎来到Spring Shell.如需帮助,请按或键入"提示",然后按ENTER键.
弹簧壳>
我们正在构建一个 spring-shell 应用程序。目前,在交互模式下,我必须像
java -jar sample.jar
Run Code Online (Sandbox Code Playgroud)
shell:> mycommand arg1 agr2
有没有办法以交互方式运行这个修女,像这样
java -jar sample.jar mycommand arg1 agr2然后退出。
我是Spring的新手,所以经过一整天的尝试,我需要问;)
是否可以将Spring引导和Spring shell结合在一起?
我的用例是构建一个包含webapp的jar(默认情况下Spring-boot嵌入jetty或tomcat),同时能够从shell执行一些项目命令.Quarts不是一个选择.如果这些命令和webapp共享相同的应用程序上下文,那将是很好的.
我的src/main/java中有两个类(加上其他目录中的一些命令和控制器)
Application.java
package dk.mrok.carmonitor;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* Bootstrap webapp
*/
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Run Code Online (Sandbox Code Playgroud)
Cli.java
package dk.mrok.carmonitor;
import java.io.IOException;
import org.springframework.shell.Bootstrap;
public class Cli {
/**
* Main class that delegates to Spring Shell's Bootstrap class in order to simplify debugging inside an IDE
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException …Run Code Online (Sandbox Code Playgroud) 我偶然发现了这个问题,解释了如何在使用单个命令从命令行调用Spring Shell应用程序后退出.但是,使用Spring Boot在2.0.0中测试它,似乎不再是用命令参数调用JAR将执行该命令然后退出的情况.shell只是正常启动而不执行提供的命令.是否仍然可以这样做?如果没有,是否可以将参数从JAR执行传递给Spring Shell,然后在执行后触发退出?
例如,假设我有一个命令,import它有几个选项.它可以在shell中运行,如下所示:
$ java -jar my-app.jar
> import -f /path/to/file.txt --overwrite
Successfully imported 'file.txt'
> exit
Run Code Online (Sandbox Code Playgroud)
但是为了构建一个可以利用这个功能的脚本,能够简单地执行和退出会很好:
$ java -jar my-app.jar import -f /path/to/file.txt --overwrite
Successfully imported 'file.txt'
Run Code Online (Sandbox Code Playgroud) 有没有办法使用 spring-shell 在同一行上发送多个命令。例如:
shell> add 1 2; add 3 4
3
7
Run Code Online (Sandbox Code Playgroud)
我注意到如果我从 运行我的应用程序Intellij,我可以复制粘贴几个命令,它会正常工作:
add 1 2
add 3 4
Run Code Online (Sandbox Code Playgroud)
但是当我在 bash 中运行可执行 jar 时它不起作用。我想这是因为它Terminal是不同的。在Intellij它的 a 中DumbTerminal,当我在 bash 中运行它时,它是一个PosixSysTerminal
我目前正在尝试改进 Spring Shell 应用程序,而使其变得更好的一件事是它是否支持值和选项的制表符补全。
举个例子,如果我的 CLI 有一个命令getInfo --name <name>,其中<name>是数据库中有限名称集中的一个名称,那么能够很方便地执行以下操作
> getInfo --name Lu<tab>
Lucy Luke Lulu
> getInfo --name Luk<tab>
> getInfo --name Luke
Run Code Online (Sandbox Code Playgroud)
有没有办法使用现有的 Spring Shell 工具来做到这一点?我浏览了文档,但找不到任何有关自动完成值的信息。
干杯!
我正在使用 Spring Shell 运行一些长时间运行的命令。无论如何,是否可以在不退出 shell 的情况下使用 Control+C 命令中断 Spring Shell 中正在运行的命令。
我想在spring-shell中着色我的控制台输出.就像是:
System.out.println("\red; red text");
Run Code Online (Sandbox Code Playgroud)
我知道彩色JUL日志记录输出,但它不是.
我怎么能这样做?
spring-boot使用的CRaSH项目可以为Spring提供交互式shell.
我发现spring-shell也有类似的功能.
那么他们之间有什么区别?为什么不使用spring-sehll进行弹簧启动?
任何意见或反馈将不胜感激.
是否可以启动Spring-Shell应用程序的特定命令,然后在命令完成后返回/退出shell?还可以将应用程序的退出代码(System.exit)公开给操作系统shell吗?
为了我的目的,我将利用插件机制和Spring-Shell的CLI-Annotations.但一般情况下,应用程序没有人工交互,而是作业调度程序(UC4)将启动应用程序并检查退出代码以在退出代码不等于0的情况下生成电子邮件.另一方面,用于手动测试我们的客户还需要完成标签,使用帮助等.
我正在尝试构建一个 Spring Shell 应用程序。我能够成功运行我的应用程序,但它会立即退出@启动并且不等待用户输入。JLineShell promptLoop 方法中似乎没有保留。
我正在用 mainClassName = "org.springframework.shell.Bootstrap" 构建一个 jar。
我的 spring-shell-plugin.xml 如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<context:component-scan base-package="com.zailab" />
</beans>
Run Code Online (Sandbox Code Playgroud)
我的主课:
public class Main {
public static void main(String[] args) throws IOException{
Bootstrap.main(args);
}
}
Run Code Online (Sandbox Code Playgroud)
我的命令类:
@Component
public class BuildCommand implements CommandMarker {
@CliAvailabilityIndicator({"echo"})
public boolean isCommandAvailable() {
return true;
}
@CliCommand(value = "echo", help = "Echo a message")
public String echo(
@CliOption(key = { "", "msg" }, mandatory …Run Code Online (Sandbox Code Playgroud)