工作这么多年了,权且大胆的预测,现在绝大部分java项目都在用Spring,大家对Spring 的理解还一直是AOP和IOC和Spring MVC的知识吧,其实Spring早就做了重构,在IOC的基础(Spring Context)上做了很多有用的东西,Spring Shell就是这样一个东西。
Spring Shell可以让你的项目很轻松的支持命令行的功能,可能有些人还觉得比较抽象,这么说假如你想让你的项目支持动态启动和停止,即java -jar xxx.jar start 就启动某个服务,然后输入stop就停止,最简单的方式就是解析main方法中的args ,然后调用不同的服务。
其实Spring Shell做的也是类似的东西,只是可以通过一些注解让你很轻松的把注解和方法之间建立联系。
看看下面官网给出的例子
@CliCommand(value = "hw simple", help = "Print a simple hello world message") public String simple( @CliOption(key = { "message" }, mandatory = true, help = "The hello world message") final String message, @CliOption(key = { "location" }, mandatory = false, help = "Where you are saying hello", specifiedDefaultValue="At work") final String location) { simpleCommandExecuted = true; return "Message = [" + message + "] Location = [" + location + "]"; }
程序启动后,输入
hw simple --message hell
其实就调用到上面的方法中了。
我跑起来了一个最简单的例子,现在贴出来供大家理解
package com.tang.test.sprintShell; import static org.junit.Assert.assertEquals; import org.junit.Test; import org.springframework.shell.Bootstrap; import org.springframework.shell.core.CommandResult; import org.springframework.shell.core.JLineShellComponent; public class HelloWorldCommandTest { @Test public void testSimple() { Bootstrap bootstrap = new Bootstrap(); JLineShellComponent shell = bootstrap.getJLineShellComponent(); CommandResult cr = shell.executeCommand("hw simple --message hello"); assertEquals(true, cr.isSuccess()); assertEquals("Message = [hello] Location = [null]", cr.getResult()); } }
我们使用Spring Shell主要写的东西。
<?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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="com.tang.test.springShell" /> </beans>
配置文件
package com.tang.test.sprintShell; import static org.junit.Assert.assertEquals; import org.junit.Test; import org.springframework.shell.Bootstrap; import org.springframework.shell.core.CommandResult; import org.springframework.shell.core.JLineShellComponent; public class HelloWorldCommandTest { @Test public void testSimple() { Bootstrap bootstrap = new Bootstrap(); JLineShellComponent shell = bootstrap.getJLineShellComponent(); CommandResult cr = shell.executeCommand("hw simple --message hello"); assertEquals(true, cr.isSuccess()); assertEquals("Message = [hello] Location = [null]", cr.getResult()); } }
测试类
ps ,注意配置文件需要放在 resources/META-INF下,至于为什么我们看看Bootstrap源码就知道了
public class Bootstrap { private static final String[] CONTEXT_PATH = new String[]{"classpath*:/META-INF/spring/spring-shell-plugin.xml"}; //后面略
最后附上pom文件
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.springframework.shell.samples</groupId> <artifactId>helloworld</artifactId> <version>1.1.0.RELEASE</version> <packaging>jar</packaging> <name>helloworld</name> <url>http://maven.apache.org</url> <properties> <spring.shell.version>1.2.0.RC1</spring.shell.version> <jar.mainclass>org.springframework.shell.Bootstrap</jar.mainclass> <log4j.version>1.2.17</log4j.version> <junit.version>4.10</junit.version> </properties> <dependencies> <dependency> <groupId>org.springframework.shell</groupId> <artifactId>spring-shell</artifactId> <version>${spring.shell.version}</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit-dep</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.5</source> <target>1.5</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <executions> <execution> <id>copy-dependencies</id> <phase>prepare-package</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <outputDirectory>${project.build.directory}/lib</outputDirectory> <overWriteReleases>true</overWriteReleases> <overWriteSnapshots>true</overWriteSnapshots> <overWriteIfNewer>true</overWriteIfNewer> </configuration> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> <useUniqueVersions>false</useUniqueVersions> <classpathPrefix>lib/</classpathPrefix> <mainClass>${jar.mainclass}</mainClass> </manifest> <manifestEntries> <version>${project.version}</version> </manifestEntries> </archive> </configuration> </plugin> </plugins> </build> <repositories> <repository> <id>libs-milestone</id> <url>http://repo.spring.io/libs-milestone/</url> </repository> <repository> <id>libs-release</id> <url>http://repo.spring.io/libs-release/</url> </repository> </repositories> </project>
希望对你有所帮助,谢谢大家。
相关推荐
Spring Boot Ssh Shell ssh的Spring Boot应用程序中的Spring Shell 有关更多信息,请访问spring ... 您可以使用以下属性禁用它们: spring : autoconfigure : exclude : - org.springframework.shell.jline.JLineShell
spring-shell的jar包,包含1.1.0和1.2.0两个版本的
spring-shell-1.1.0.RC1-dist
弹簧壳3 Spring Shell 3是一项仅依赖于Spring Boot 2.x的工作,并且不尝试与旧版Spring Shell 1.x或Spring Boot 1.x保持任何向后兼容性。建造 ./mvnw package跑步该项目附带一个示例应用程序,展示了您可以编写命令...
Spring Cloud Function 是基于Spring Boot 的函数计算框架,它抽象出所有传输细节和...反弹shell脚本:python Spel_RCE_Bash_EXP.py url lhost lport 受影响版本:3.0.0.RELEASE <= Spring Cloud Function <= 3.2.2
提到 Java,大家都会想到 Java 在服务器端应用开发中的使用。...Spring Shell 可以帮助简化这些常见的任务,让开发人员专注于实现应用的业务逻辑。本文对 Spring Shell 进行详细的介绍。 Spring Shell 入门 最简
Linux下SpringBoot/SpringCloud微服务部署Shell,对微服务jar进行部署,格式如下: sh spring-boot.sh $OPERATION jar包.jar --spring.profiles.active={自定义} OPERATION支持start、stop、status
弹簧壳样品这是一个使用 Spring Shell(当前版本为 1.1.0-RELEASE)的参考项目。 它使用 maven 来编译、测试、构建和部署。Maven 用例编译/测试/安装mvn clean install运行(通过 Maven) mvn exec:java构建可执行...
本shell脚本可以便捷的启动或停止java应用程序包含(spring-boot),且支持重启、停止、查看日志登命令。免去使用kill命令结束进程!
Java SSM项目是一种使用Java语言和SSM框架(Spring + Spring MVC + MyBatis)开发的Web应用程序。SSM是一种常用的Java开发框架组合,它结合了Spring框架、Spring MVC框架和MyBatis框架的优点,能够快速构建可靠、...
Spring4Shell的漏洞原理分析.doc
什么是Spring Cloud Skipper Shell? 一个软件包管理器,可以在多个云平台上安装,升级和回滚Spring Boot应用程序。 船长可以用作实施持续部署实践的一部分。 该docker映像附带了官方Shell与船长服务器进行交互。 ...
1.2.2 使用Spring Initializr初始化Spring Boot项目 10 1.3 小结 18 第2章 开发第一个应用程序 19 2.1 运用Spring Boot 19 2.1.1 查看初始化的Spring Boot新项目 21 2.1.2 Spring Boot项目构建过程解析 ...
project-commandline:使用Spring Shell的命令行项目
将脚本放入jar包同级,linux cd到目录下 sh boot.sh start 启动所有 usage查看可用命令
主要介绍了Spring Boot调用 Shell 脚本实现看门狗功能,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
弹簧 XD 外壳运行 Spring XD Shell 的 Docker 映像。 当前没有任何方法可以在启动 shell 时指定管理 URL(请参阅),因此您需要使用 xd:>admin config server http://host:ip连接到正在运行的管理服务器。 例如,...
linux服务器,springboot,spring cloud、spring cloud alibaba等项目启动脚本 下载脚本, 1,上传脚本至jar包同级目录 2,更改脚本: jar包名称 项目文件路径 日志路径(包含日志名称) 脚本已配置好jvm优化...